Partager via


XmlNode question...

Ok, tonights question of the evening is this...

I have a method which takes an XmlNode as a parameter.  This method then proceeds to process the information in the XmlNode but there's one particular node that "could" be present.  I'm trying to do a check against null on the node, but I keep getting object not set to refernce of an object...which I can understand.  Is there an easy way to check if this node exists and if so do something with it?  I'm looking through MSDN and must be just missing it.

Anyone got any suggestions?

Comments

  • Anonymous
    April 07, 2005
    Well,I think you can check whether the Object variable is null or not,i am not sure you tried these methods or not

    XmlNode mynode=doc.SelectSingleNode(yourNodeVariabke);

    if(mynode==null)
    {
    Console.Write("i am not present")
    }

    the other option is you can try XMLNodelist`s Count property

    XmlNodeList irisNodeTitle = doc.SelectNodes("//result/category[text()='"+tmp+"']//parent::node()/title");
    Console.WriteLine(irisNodeTitle.Count.ToString());


    HIH

    -adnan
  • Anonymous
    April 07, 2005
    What is your code?
    Something like this should work:

    XmlNode n = yourNode.selectSingleNode("optional");
    if(n != null)
    {
    // optional is present
    }

    Bye,
    SvenC
  • Anonymous
    July 14, 2005
    Could it be that the node is present (not null) and that you're using an innertext property or something on it when in fact it's an empty node? So something like this : <blah /> as opposed to <blah></blah>