locked
xmldocument.selectsinglenode always returns null RRS feed

  • Question

  • User-1369888382 posted

    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?

    Tuesday, January 15, 2013 8:46 AM

Answers

  • User-1369888382 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2013 4:02 AM

All replies

  • User281315223 posted

    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>).

    Tuesday, January 15, 2013 9:02 AM
  • User-1369888382 posted

    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.

    Tuesday, January 15, 2013 11:53 PM
  • User281315223 posted

    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"



    Wednesday, January 16, 2013 12:14 AM
  • User-1369888382 posted

    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.

     

     

    Wednesday, January 16, 2013 12:50 AM
  • User281315223 posted

    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 : "";




    Wednesday, January 16, 2013 1:04 AM
  • User-1369888382 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 16, 2013 4:02 AM
  • User3866881 posted

    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.

    Reguards!

    Wednesday, January 16, 2013 8:23 PM