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 a non declared prefix.
Here is the XML line the exception refers to:
<use xlink:href="#lend13" transform="scale(-8.5,-8.5) "/>
How can I modify the loading code, so that the XML document will be read successfully? Do I have to set up namespaces beforehand? How?
The most important advantage of LINQ to XML is its integration with Language-Integrated Query (LINQ). This integration enables you to write queries on the in-memory XML document to retrieve collections of elements and attributes.
The LINQ to XML will bring the XML document into memory and allows us to write LINQ Queries on in-memory XML document to get the XML document elements and attributes. To use LINQ to XML functionality in our applications, we need to add "System. Xml. Linq" namespace reference.
XDocument is from the LINQ to XML API, and XmlDocument is the standard DOM-style API for XML. If you know DOM well, and don't want to learn LINQ to XML, go with XmlDocument .
I think this will be helpful it worked for me...
http://aspnetgotyou.blogspot.com/2010/06/xdocument-or-xelement-with-xmlnamespace.html
if you can edit the Xml, you can fix by defining the 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 to XML you can define the attribute using XNamesace
XNamespace ns = "http://myurl.com/";
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