I have this code :
/*string theXml = @"<Response xmlns=""http://myvalue.com""><Result xmlns:a=""http://schemas.datacontract.org/2004/07/My.Namespace"" xmlns:i=""http://www.w3.org/2001/XMLSchema-instance""><a:TheBool>true</a:TheBool><a:TheId>1</a:TheId></Result></Response>";*/ string theXml = @"<Response><Result><TheBool>true</TheBool><TheId>1</TheId></Result></Response>"; XDocument xmlElements = XDocument.Parse(theXml); var elements = from data in xmlElements.Descendants("Result") select new { TheBool = (bool)data.Element("TheBool"), TheId = (int)data.Element("TheId"), }; foreach (var element in elements) { Console.WriteLine(element.TheBool); Console.WriteLine(element.TheId); }
When I use the first value for theXml, the result is null, whereas with the second one, I have good values ...
How to use Linq to Xml with xmlns values ?
LINQ to XML is an XML programming interface. LINQ to XML is a LINQ-enabled, in-memory XML programming interface that enables you to work with XML from within the . NET programming languages. LINQ to XML is like the Document Object Model (DOM) in that it brings the XML document into memory.
When you use multiple namespaces in an XML document, you can define one namespace as the default namespace to create a cleaner looking document. The default namespace is declared in the root element and applies to all unqualified elements in the document. Default namespaces apply to elements only, not to attributes.
One of the primary motivations for defining an XML namespace is to avoid naming conflicts when using and re-using multiple vocabularies. XML Schema is used to create a vocabulary for an XML instance, and uses namespaces heavily.
Adding a namespace prefix to an elementThe XQuery expression in the following SELECT statement adds a namespace prefix to an element. The XQuery expression uses fn:QName to create a QName with a namespace binding. The XQuery let clause creates an empty element with name emp and namespace http://example.com/new .
LINQ to XML methods like Descendants
and Element
take an XName
as an argument. There is a conversion from string
to XName
that is happening automatically for you. You can fix this by adding an XNamespace
before the strings in your Descendants
and Element
calls. Watch out because you have 2 different namespaces at work.
string theXml = @"true1"; //string theXml = @"true1"; XDocument xmlElements = XDocument.Parse( theXml ); XNamespace ns = "http://myvalue.com"; XNamespace nsa = "http://schemas.datacontract.org/2004/07/My.Namespace"; var elements = from data in xmlElements.Descendants( ns + "Result" ) select new { TheBool = (bool) data.Element( nsa + "TheBool" ), TheId = (int) data.Element( nsa + "TheId" ), }; foreach ( var element in elements ) { Console.WriteLine( element.TheBool ); Console.WriteLine( element.TheId ); }
Notice the use of ns in Descendants
and nsa in Elements
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With