ProfK
ProfK

Reputation: 51084

How to query an EF model (EDMX document) with XPath

I'm trying to write a utility that automatically sets the ProviderManifestToken attribute in an EDMX document Schema element, but even my basic XPath is not working. What am I doing wrong?

The XML:

<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
  <!-- EF Runtime content -->
  <edmx:Runtime>
    <!-- SSDL content -->
    <edmx:StorageModels>
    <Schema Namespace="PvmmsModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005

My attempt:

        var edmx = new XmlDocument();            
        edmx.Load(@"C:\Development\Provantage\PvmmsApp\Model.edmx");
        var nsm = new XmlNamespaceManager(edmx.NameTable);
        nsm.AddNamespace("edmx", "http://schemas.microsoft.com/ado/2007/06/edmx");

        var x = edmx.SelectSingleNode("//edmx:Edmx/edmx:Runtime/edmx:StorageModels", nsm);

This works, but as soon as I append Schema to the query. Then I get a null result.

Upvotes: 3

Views: 1278

Answers (1)

Pavel Minaev
Pavel Minaev

Reputation: 101605

Here's how a complete Schema element actually looks (your snippet seems to be trimmed);

<Schema xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl"
        Namespace="Model1.Store"
        Alias="Self"
        Provider="System.Data.SqlClient"
        ProviderManifestToken="2005">

Note the xmlns part. So it's actually in a namespace, despite the lack of prefix.

Now, in XPath, the lack of prefix always means "not in any namespace". So you'll need to bind some prefix specifically for your XPath, and use that in the query:

...
nsm.AddNamespace("ssdl", "http://schemas.microsoft.com/ado/2009/02/edm/ssdl");
var x = edmx.SelectSingleNode(
    "//edmx:Edmx/edmx:Runtime/edmx:StorageModels/ssdl:Schema", nsm)

Upvotes: 7

Related Questions