开发者

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.


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜