【问题标题】:Error when accessing xml nodes访问 xml 节点时出错
【发布时间】:2012-07-19 07:48:03
【问题描述】:

我有一个xml如下

<Students xmlns="http://AdapterTest">
<Schema name="Schema1" xmlns="urn:schemas-microsoft-com:xml-data" xmlns:dt="urn:schemas-microsoft-com:datatypes">
    <ElementType name="Student" content="empty" model="closed">
        <AttributeType name="Student_id" dt:type="string"/>
        <AttributeType name="Student_Name" dt:type="string"/>
        <attribute type="Student_id"/>
        <attribute type="Student_Name"/>
    </ElementType>
</Schema>
<Student Student_id="123456" Student_Name="Udaya" xmlns="x-schema:#Schema1"/>
<Student Student_id="568923" Student_Name="Christie" xmlns="x-schema:#Schema1"/>
<Student Student_id="741852" Student_Name="Test Name" xmlns="x-schema:#Schema1"/>
<Student Student_id="852789" Student_Name="Sample Name" xmlns="x-schema:#Schema1"/>
</Students>

在我的应用程序中,我尝试使用 LINQ 访问节点,如下所示。

XDocument xdoc1 = XDocument.Load("Customer.xml");

        List<Student> studentList =
            (from _student in xdoc1.Element("Students").Elements("Student")
             select new Student
             {
                 firstName = _student.Attribute("Student_id").Value,
                 lastName = _student.Attribute("Student_Name").Value,


             }).ToList();

它给了我一个未设置为对象实例的对象引用。当我从根元素中删除 xmlns="http://AdapterTest" 时,它工作正常。我在这里缺少什么

【问题讨论】:

  • 可能是复制和粘贴错误,但您是否关闭了 标签?

标签: c# xml linq


【解决方案1】:

我以前也遇到过类似的问题。就像你说的,如果你从 xml 中删除命名空间,它就可以工作。你必须这样做:

XNamespace rootNs = xdoc1.Root.Name.Namespace;
XNamespace studentNS = "x-schema:#Schema1";

然后在选择节点时在选择器前面加上ns,比如:

var yourStudentList = xdoc1.Element(rootNS + "Student").Elements(studentNS + "Student");

我没有对此进行测试,但它看起来与我遇到的问题相似。

【讨论】:

  • 我用过这个。现在错误没有出现。但它想给我结果。列表是空的。虽然我需要获取列表中的 4 个元素
  • 那你的选择是错误的。使用您在 xml 中的命名空间,即根命名空间和学生命名空间。我会为你更新。
  • @udaya726 查看我的更新。您必须使用正确的命名空间迭代 yourStudentList 才能工作。
【解决方案2】:

添加命名空间:

        XNamespace ns1 = xdoc1.Root.GetDefaultNamespace();
        XNamespace ns2 = "x-schema:#Schema1";

并在检索元素时使用它们:

        List<Student> studentList =
            (from _student in xdoc1.Element(ns1 + "Students")
                                   .Elements(ns2 + "Student")
             select new Student
             {
                 firstName = _student.Attribute("Student_id").Value,
                 lastName = _student.Attribute("Student_Name").Value,
             }).ToList();

您将获得包含四个元素的列表。

【讨论】:

    【解决方案3】:

    您需要为 LINQ 定义一个命名空间。

    XDocument xdoc1 = XDocument.Load("Customer.xml");
    XNamespace ns = "http://AdapterTest"; //definine namespace
    
        List<Student> studentList =
            (from _student in xdoc1.Element(ns + "Students").Elements("Student")
             select new Student
             {
                 firstName = _student.Attribute("Student_id").Value,
                 lastName = _student.Attribute("Student_Name").Value,
    
    
             }).ToList();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 1970-01-01
      相关资源
      最近更新 更多