Reading an XML Document Using Linq

I would like to read an XML document using the following code:

XDocument xdoc = XDocument.Load(fileName); 

This does not work, and the following exception is thrown (freely translated by me):

System.Xml.XmlException: "xlink" is not an advertised prefix.

Here is the XML line to which the exception relates:

 <use xlink:href="#lend13" transform="scale(-8.5,-8.5) "/> 

How do I change the download code so that the XML document is read successfully? Should I create namespaces in advance? How?

+5
source share
2 answers

if you can edit Xml, you can fix it by specifying a namespace for it

 <use xlink:href="#lend13" transform="scale(-8.5,-8.5) xmlns:xlink="http://myurl.com/" /> 

otherwise, you can predefine the namespace when using XmlDocument

 XmlDocument.DocumentElement.SetAttribute("xmlns:xlink", "http://myurl.com/"); 

and in linq for XML you can define an attribute using XNamesace

 XNamespace ns = "http://myurl.com/"; 
+2
source

All Articles