SelectNodes return null

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
<br/>
I have sample code that retrieves value from xml string, below:<br/>
<br/>
<br/>
string xmlstring=<br/>
@"<?xml version=""1.0"" encoding=""utf-8""?><br/>
<SubjectsViewModel xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema" <br/>
<Subjects><br/>
<SubjectModel><br/>
<Id>4</Id><br/>
<ThemeId>1</ThemeId><br/>
<SubjectTexts><br/>
<SubjectTextModel><br/>
<Id>7</Id>
<br/>
</SubjectTextModel><br/>
</SubjectTexts><br/>
</SubjectModel><br/>
</Subjects><br/>
</SubjectsViewModel>";<br/>
<br/>
XmlDataDocument xml = new XmlDataDocument();<br/>
xml.LoadXml(xmlstring);<br/>
<br/>
XmlElement root = xml.DocumentElement;<br/>
<br/>
XmlNodeList nodelist = root.SelectNodes("/SubjectsViewModel/Subjects/SubjectModel");<br/>
foreach (XmlNode item in nodelist)<br/>
{<br/>
string id = item.SelectSingleNode("./Id").InnerXml;<br/>
string subjectTitle = item.SelectSingleNode("./SubjectTexts/SubjectTextModel/Id").InnerXml;<br/>
Console.WriteLine(id+" "+subjectTitle);<br/>
}<br/>
<br/>
It works on my pc, but it failed on server with the error:<br/>
<br/>
Object reference not set to an instance of an object<br/>
<br/>
On<br/>
<br/>
foreach (XmlNode item in nodelist)<br/>

The default namespace is being used. Is it something to do with namespace?

Any idea would be very much appreciated!

View the full article
 
Back
Top