Banshee
Banshee

Reputation: 15827

Getting decendant node that contains ":"?

I have loaded a XML into a XDocument that looks something like this :

<MyXML xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/MyApp.Client.Main.GUI.Report">
  <Wrapper xmlns:d2p1="http://schemas.datacontract.org/2004/07/MyApp.Business.Entity">
    <d2p1:_MultipelAttributId>156</d2p1:_MultipelAttributId>
  </Wrapper>
</MyXML>

Now I need to get the value of _MultipelAttributId but when stating this :

XElement element = (from cml2 in doc.Descendants("d2p1:_MultipelAttributId") select cml2).FirstOrDefault();

I get exception about the ":"?

So how do I get the valiue?

Upvotes: 0

Views: 114

Answers (3)

Buh Buh
Buh Buh

Reputation: 7546

This MSDN article explains well how to work with namespaces in Linq to XML http://msdn.microsoft.com/en-us/library/bb669152.aspx

XNamespace d2p1 = "http://schemas.datacontract.org/2004/07/MyApp.Business.Entity";
XElement multipelAttributId = doc.Descendants(d2p1 + "_MultipelAttributId").FirstOrDefault();

Notice that you can seamly create an XNamespace or an XName from a string because Microsoft have writen inplicit casts for these types.

Upvotes: 1

L.B
L.B

Reputation: 116168

XNamespace ns = "http://schemas.datacontract.org/2004/07/MyApp.Business.Entity"; 

var element = xDoc.Descendants(ns + "_MultipelAttributId").FirstOrDefault();

Upvotes: 3

Fung
Fung

Reputation: 3558

The element name you're querying is not correct. Try this:

XElement element = (
    from cml2 in doc.Descendants()
    where cml2.Name.LocalName == "_MultipelAttributId"
    select cml2).FirstOrDefault();

Upvotes: 1

Related Questions