Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 719
ProfK Avatar asked Dec 04 '25 17:12

ProfK


1 Answers

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)
like image 83
Pavel Minaev Avatar answered Dec 06 '25 07:12

Pavel Minaev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!