Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Linq to Xml with Xml namespaces

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 ?

like image 930
Tim Avatar asked Feb 26 '10 08:02

Tim


People also ask

Can we use LINQ for XML?

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.

Can XML have multiple namespaces?

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.

Can an XML schema define namespaces?

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.

How do I add namespace prefix to XML element?

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 .


1 Answers

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

like image 151
Mike Two Avatar answered Sep 27 '22 20:09

Mike Two