XmlDocument doc = new XmlDocument();
doc.LoadXml("<mail><res><stat><msg><test></test></msg></stat></res></mail>");
string err = doc.SelectSingleNode("mail/res/stat/msg").InnerText;
and revising it to handle null checking - you can use :
//Checks for any nulls - if none are found it will output properly, otherwise it will output ""
string err = (doc.SelectSingleNode("mail/res/stat/msg") != null) ? doc.SelectSingleNode("mail/res/stat/msg").InnerText : "";
Since I can't change the doc.SelectSingleNode statement,I used the following code:
XmlDocument doc =
new XmlDocument();
doc.LoadXml("res><stat><msg><test></test></msg></stat></res>");
string err = doc.SelectSingleNode("res/stat/msg").InnerText;
This works.
Hi,
Since your problem is solved. I'll mark it as an answer to close this thread.
Anything feedback, please feel free to continue by creating another new thread.
dotnet16
Member
155 Points
113 Posts
xmldocument.selectsinglenode always returns null
Jan 15, 2013 12:46 PM|LINK
Here is my code:
XmlDocument doc = new XmlDocument();
doc.LoadXml("<mail><res><stat><msg><test>testerror</test></msg></stat></resp></mail>");
string err = doc.SelectSingleNode("res/stat/msg").InnerText;
I need to set my xml so that err is not null.But with the above code it is always null.How should I set my XML?
Rion William...
All-Star
27896 Points
4618 Posts
Re: xmldocument.selectsinglenode always returns null
Jan 15, 2013 01:02 PM|LINK
You needed your outer <mail> element including within your SelectSingleNode() method :
XmlDocument doc = new XmlDocument(); doc.LoadXml("<mail><res><stat><msg><test>testerror</test></msg></stat></res></mail>"); string err = doc.SelectSingleNode("mail/res/stat/msg").InnerText;Additionally, you had a misspelling in your second <res> tag (it was previously <resp>).
dotnet16
Member
155 Points
113 Posts
Re: xmldocument.selectsinglenode always returns null
Jan 16, 2013 03:53 AM|LINK
Rion,
Thanks for your reply.My need is such that the 'doc.SelectSingleNode("mail/res/stat/msg").InnerText;' shouldn't change.
I can vary my doc but the 'err' should never be null.
So I can't use the string err = doc.SelectSingleNode("mail/res/stat/msg").InnerText; as you suggested.
Rion William...
All-Star
27896 Points
4618 Posts
Re: xmldocument.selectsinglenode always returns null
Jan 16, 2013 04:14 AM|LINK
So you basically want to store the "deepest" element given a single XML string? If that is the case - you could use the following XPath expression :
string yourDeepestText = doc.SelectSingleNode("/*[descendant::*]").InnerText;Examples:
//Three XML Documents XmlDocument doc = new XmlDocument(); doc.LoadXml("<mail><res><stat><msg><test>error</test></msg></stat></res></mail>"); XmlDocument doc2 = new XmlDocument(); doc2.LoadXml("<asp><dot><net><rules>testing</rules></net></dot></asp>"); XmlDocument doc3 = new XmlDocument(); doc3.LoadXml("<this><is><a><test><now>ASP</now></test></a></is></this>"); //Each processed through the same expression string d1 = doc.SelectSingleNode("/*[descendant::*]").InnerText; //d1 = "error" string d2 = doc2.SelectSingleNode("/*[descendant::*]").InnerText; //d2 = "testing" string d3 = doc3.SelectSingleNode("/*[descendant::*]").InnerText; //d3 = "ASP"dotnet16
Member
155 Points
113 Posts
Re: xmldocument.selectsinglenode always returns null
Jan 16, 2013 04:50 AM|LINK
Rion,
My code for 'doc.SelectSingleNode' need not be changed.i.e., I have to change my doc so that
doc.SelectSingleNode("res/stat/msg") is not null.
Can you pls suggest me a way of setting the doc accordingly.
Rion William...
All-Star
27896 Points
4618 Posts
Re: xmldocument.selectsinglenode always returns null
Jan 16, 2013 05:04 AM|LINK
Using the original answer that I had suggested :
XmlDocument doc = new XmlDocument(); doc.LoadXml("<mail><res><stat><msg><test></test></msg></stat></res></mail>"); string err = doc.SelectSingleNode("mail/res/stat/msg").InnerText;and revising it to handle null checking - you can use :
//Checks for any nulls - if none are found it will output properly, otherwise it will output "" string err = (doc.SelectSingleNode("mail/res/stat/msg") != null) ? doc.SelectSingleNode("mail/res/stat/msg").InnerText : "";Revised Answer
XmlDocument doc = new XmlDocument(); doc.LoadXml("<mail><res><stat><msg><test></test></msg></stat></res></mail>"); string err = (doc.SelectSingleNode("mail/res/stat/msg") != null) ? doc.SelectSingleNode("mail/res/stat/msg").InnerText : "";dotnet16
Member
155 Points
113 Posts
Re: xmldocument.selectsinglenode always returns null
Jan 16, 2013 08:02 AM|LINK
Since I can't change the doc.SelectSingleNode statement,I used the following code:
XmlDocument doc = new XmlDocument();
doc.LoadXml("res><stat><msg><test></test></msg></stat></res>");
string err = doc.SelectSingleNode("res/stat/msg").InnerText;
This works.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: xmldocument.selectsinglenode always returns null
Jan 17, 2013 12:23 AM|LINK
Hi,
Since your problem is solved. I'll mark it as an answer to close this thread.
Anything feedback, please feel free to continue by creating another new thread.
Reguards!