Boris
Boris

Reputation: 8951

Reading an XML document with 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 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

Answers (2)

n00b
n00b

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

Saveendra Ekanayake
Saveendra Ekanayake

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

Related Questions