I have an xml as follows
<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>
In my application I am trying to access the nodes using LINQ as follows.
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();
It gives me an Object reference not set to an instance of an object.When I remove the xmlns=”http://AdapterTest” from the root element it works fine. What I am missing here
Ive had a similar problem before like this. Like you said if you remove the namespace from the xml it works. You have to do something like this:
And then when selecting nodes prepend the selector with ns, like:
I havent tested this but it looks similar to and issue i have had.