简体   繁体   English

读取XML和未声明的名称空间

[英]Reading XML and undeclared namespace

I got this error message when I try to read the XMLfile 当我尝试读取XMLfile时收到此错误消息

 Unhandled Exception: System.Xml.XmlException: 'xi' is an undeclared namespace. Line 12, position 18.
   at System.Xml.XmlTextReaderImpl.Throw(Exception e)
   at System.Xml.XmlTextReaderImpl.Throw(String res, String arg, Int32 lineNo, Int32 linePos)
   at System.Xml.XmlTextReaderImpl.LookupNamespace(NodeData node)
   at System.Xml.XmlTextReaderImpl.ElementNamespaceLookup()
   at System.Xml.XmlTextReaderImpl.ParseAttributes()
   at System.Xml.XmlTextReaderImpl.ParseElement()
   at System.Xml.XmlTextReaderImpl.ParseElementContent()
   at System.Xml.XmlTextReaderImpl.Read()
   at System.Xml.XmlTextReader.Read()

Code to read XML: 读取XML的代码:

   XmlTextReader reader = new XmlTextReader("file.xml");
    while (reader.Read())
    {
        // Do some work here on the data.
        Console.WriteLine(reader.Name);
    }
    Console.ReadLine();

The xml file: xml文件:

<?xml version="1.0" encoding="ISO-8859-1"?>    
<doh> 
        <!-- Available Resources-->
        <servers>
                <xi:include href="file.xml"/>
        </servers>

I assume that you are wanting to use XInclude. 我假设您要使用XInclude。 You must define which namespace the xi prefix maps to. 您必须定义xi前缀映射到的名称空间。 The easiest why is too include the namespace mapping in the root element. 最简单的原因也是在根元素中包含名称空间映射。

  <doh xmlns:xi="http://www.w3.org/2001/XInclude">

Note that the XmlTextReader will not include the "file.xml". 请注意, XmlTextReader将不包含“ file.xml”。 You will have to 'include' by some other code. 您将必须通过其他一些代码“包含”。 A good background paper on the XInclude can be found on MSDN at http://msdn.microsoft.com/en-us/library/aa302291.aspx 可以在MSDN的http://msdn.microsoft.com/zh-cn/library/aa302291.aspx上找到有关XInclude的很好的背景资料。

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

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