0

Given the following xml (simplified)

<root xmlns="http://schemas.datacontract.org/2004/07/Base" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Items>
        <item xmlns:a="http://schemas.datacontract.org/2004/07/Base" i:type="a:A"></item>
        <item xmlns:a="http://schemas.datacontract.org/2004/07/Base" i:type="a:B"></item>
    </Items>
</root>

I'm trying to do something along these line.

XNamespace xmlInstanceNs = "http://www.w3.org/2001/XMLSchema-instance";
XNamespace baseNs = "http://schemas.datacontract.org/2004/07/Base";
var items = root.Descendants(baseNs + "item");
var aItems = items.Where(i => i.Attribute(xmlInstanceNs + "type").Value == baseNs + "A");

Of course this isnt working since the last line is basically comparing the string "a:A" to the XName "{http://schemas.datacontract.org/2004/07/Base}A" which arent identical.

Is there a way to parse the "a:A" string to its XName equivalent without having to manually iterate the xml to find all the namespace abbreviations?

Benoittr
  • 4,091
  • 4
  • 27
  • 38

1 Answers1

1

There is http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.getnamespaceofprefix%28v=vs.110%29.aspx so you should be able to compare

items.Where(i => 
  baseNs  + "A" == 
  i.GetNamespaceOfPrefix(i.Attribute(xmlInstanceNs + "type").Value.Split(new Char[] { ':' })[0]) + "A")
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • That's it, except the second element of the split result should be appened at the end instead of "A". – Benoittr Jul 28 '14 at 20:21