我有一个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" 时,它工作正常。我在这里想念的