Вопрос

I'm trying to parse the value of m:MaxDataServiceVersion from the edmx xml which starts like this:

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

Following code returns the needed element

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

but all my tries to get the attribute value were not successfull. Here is what I've tried:

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");

Running the following code

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();
}

will output both attributes

m:DataServiceVersion = 1.0
m:MaxDataServiceVersion = 3.0

but it should be possible to get the needed one without to loop through all of them, I think...

So is there a way to get the m:MaxDataServiceVersion value without to loop?

Это было полезно?

Решение

The XML starts with

<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">

so one way to access the attribute of each DataServices element is

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");
}

If you want the attribute value of the first such attribute then doing

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

should suffice.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top