Reputation: 8951
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?
Upvotes: 5
Views: 274
Reputation: 1852
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/";
Upvotes: 2
Reputation: 3313
I think this will be helpful it worked for me...
http://aspnetgotyou.blogspot.com/2010/06/xdocument-or-xelement-with-xmlnamespace.html
Upvotes: 4