【问题标题】:Loading XML with XDocument when xsi:nilxsi:nil 时使用 XDocument 加载 XML
【发布时间】:2016-05-10 01:51:05
【问题描述】:

我正在尝试用 C 语言的 XDocument 阅读这个 XML 文档。

  <Instrument_Root>
    <Instrument_ID>123</Instrument_ID>
    <Deal_number xsi:nil="true"/>
  </Instrument_Root>

我使用下面的代码来阅读 XML 文档:

   XDocument xDoc = XDocument.Load("XMLFile1.xml");

由于 xsi:nil,我收到一条错误消息

错误消息:未声明的 System.Xml.dll 'xsi' 中发生了“System.Xml.XmlException”类型的未处理异常。

有没有办法将 xsi 视为 NULL?

谢谢

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    命名空间前缀xsi 必须在某处声明,文档才能被限定为 XML,例如:

    <Instrument_Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Instrument_ID>123</Instrument_ID>
        <Deal_number xsi:nil="true"/>
    </Instrument_Root>
    

    【讨论】:

      【解决方案2】:

      您可以尝试将 XML 文件更改为如下所示:

      <Instrument_Root  xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
          <Instrument_ID>123</Instrument_ID>
          <Deal_number xsi:nil="true"/>
      </Instrument_Root>
      

      screencapture

      【讨论】:

      • 不要将链接发布为答案,而是添加一些文本来解释此答案如何帮助 OP 解决当前问题。谢谢
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-01
      • 2021-12-12
      • 2013-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多