0

我正在尝试从edmx xml解析 m:MaxDataServiceVersion 的值,它的开头如下所示:

<edmx:Edmx Version="1.0">
<edmx:DataServices m:DataServiceVersion="1.0" m:MaxDataServiceVersion="3.0">
<Schema Namespace="NorthwindModel">
<EntityType Name="Category">

以下代码返回所需的元素

string nav = navigator.SelectSingleNode("//*[local-name() = 'DataServices']");

但是我所有获取属性值的尝试都没有成功。这是我尝试过的:

string nav = navigator.SelectSingleNode("//*[local-name() = 'DataServices']").GetAttribute("@{m}MaxDataServiceVersion", "");
nav = navigator.SelectSingleNode("//*[local-name() = 'DataServices']").GetAttribute("m:MaxDataServiceVersion", "");
nav = navigator.SelectSingleNode("//*[local-name() = 'DataServices']").GetAttribute("MaxDataServiceVersion", "");
nav = navigator.SelectSingleNode("//*[local-name() = 'DataServices']").GetAttribute("MaxDataServiceVersion", "m");
nav = navigator.SelectSingleNode("//*[local-name() = 'DataServices']").GetAttribute("MaxDataServiceVersion", "edmx:m");

运行以下代码

XPathNodeIterator nodes = navigator.Select("//*[local-name() = 'DataServices']");
while (nodes.MoveNext())
{
    XPathNavigator navigator2 = nodes.Current.Clone();
    navigator2.MoveToFirstAttribute();
    Console.WriteLine("{0} = {1}", navigator2.Name, navigator2.Value);

    while (navigator2.MoveToNextAttribute())
    {
        Console.WriteLine("{0} = {1}", navigator2.Name, navigator2.Value);
    }

    Console.WriteLine();
}

将输出两个属性

m:DataServiceVersion = 1.0
m:MaxDataServiceVersion = 3.0

但是我认为应该可以在不遍历所有这些的情况下获得所需的...

那么有没有办法在m:MaxDataServiceVersion不循环的情况下获得价值?

4

1 回答 1

1

XML 以

<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"><edmx:DataServices m:DataServiceVersion="1.0" m:MaxDataServiceVersion="3.0" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">

所以访问每个DataServices元素的属性的一种方法是

XmlNamespaceManager nsMgr = new XmlNamespaceManager(navigator.NameTable);
nsMgr.Add("edmx", "http://schemas.microsoft.com/ado/2007/06/edmx");

foreach (XPathNavigator dataServices in navigator.Select("edmx:DataServices", nsMgr))
{
  string version = dataServices.GetAttribute("MaxDataServiceVersion", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
}

如果您想要第一个此类属性的属性值,则执行

nsMgr.Add("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata");
string version = navigator.SelectSingleNode("//edmx:DataServices/@m:MaxDataServiceVersion", nsMgr).Value;

应该足够了。

于 2013-06-10T16:47:03.490 回答