简体   繁体   中英

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?

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/";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM