简体   繁体   English

使用Linq读取XML文档

[英]Reading an XML document with Linq

I would like to read an XML document using the following code: 我想使用以下代码阅读XML文档:

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. System.Xml.XmlException:'xlink'是未声明的前缀。

Here is the XML line the exception refers to: 这是异常引用的XML行:

<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? 如何修改加载代码,以便成功读取XML文档? Do I have to set up namespaces beforehand? 我必须预先设置名称空间吗? How? 怎么样?

if you can edit the Xml, you can fix by defining the namespace for it 如果您可以编辑Xml,则可以通过为其定义名称空间来进行修复

<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时预定义名称空间

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

and in linq to XML you can define the attribute using XNamesace 在linq to XML中,您可以使用XNamesace定义属性

XNamespace ns = "http://myurl.com/";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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