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?
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")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.