Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在C#项目中使用引用的XML架构文件(xsd)_C#_Xml_Xsd_Getelementbyid - Fatal编程技术网

无法在C#项目中使用引用的XML架构文件(xsd)

无法在C#项目中使用引用的XML架构文件(xsd),c#,xml,xsd,getelementbyid,C#,Xml,Xsd,Getelementbyid,我定义了一个非常简单的XML模式,名为MySchema,我将其添加到我的项目的参考中: <?xml version="1.0" encoding="utf-8"?> <xs:schema id="MySchema" elementFormDefault="qualified" attributeFormDefault="unqualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <

我定义了一个非常简单的XML模式,名为
MySchema
,我将其添加到我的项目的参考中:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="MySchema"
    elementFormDefault="qualified"
    attributeFormDefault="unqualified"
    xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="UserList">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="User" type="xs:string" minOccurs="0" maxOccurs="unbounded">
        </xs:element>
      </xs:sequence>
      <xs:attribute name="id" type="xs:ID" use="required"/>
    </xs:complexType>
  </xs:element>

</xs:schema>
但是,当我运行
GetElementById(“local”)
时,仍然会得到一个“NullReferenceException”


我想我需要做的不仅仅是将xsd文件添加到引用中。有人能告诉我哪里出错了吗?

如果没有定义DTD或XSD来定义
id
是什么,那么
GetElementById
方法就不起作用

如果在未显示的代码中定义XSD或DTD,请更新问题

DOM实现必须具有定义哪些属性属于类型ID的信息。尽管类型ID的属性可以在XSD架构或DTD中定义,但此版本的产品仅支持在DTD中定义的属性。名为“ID”的属性不是ID类型,除非在DTD中定义。不知道属性是否为ID类型的实现将不返回任何内容

基于本文顶部的XSD,我认为您没有将XSD加载到XmlDocument实例中,以便它知道哪个是
xs:id

看起来不支持XSD模式中定义的id属性,只有DTD:

DOM实现必须具有定义哪些属性属于类型ID的信息。尽管类型ID的属性可以在XSD架构或DTD中定义,但此版本的产品仅支持在DTD中定义的属性。名为“ID”的属性不是ID类型,除非在DTD中定义。属性是否为ID类型未知的实现应返回null

您可以对传统XPath使用
SelectSingleNode
,但是:

doc.DocumentElement.SelectSingleNode("//UserList[@id='local']")

如何获得XmlDocument的实例?@Maslow
XmlDocument doc=new XmlDocument()
我不太确定如何使用需要
XmlNameTable
的构造函数,我希望
能做到这一点。肯定忽略了你发布的信息。是的,我没有在XmlDocument中显式加载XSD——我认为只需将文件添加到引用中就足够了。工作非常完美!但是,不支持将太糟糕的ID作为XSD架构中定义的属性。
doc.DocumentElement.SelectSingleNode("//UserList[@id='local']")