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?

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

1 Comment

That's it, except the second element of the split result should be appened at the end instead of "A".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.