selecting a child

Hello,

I want to find a single node by name. I'm using the SelectSingleNode method of the XmlDocument object

the problem is that when the XML contains child elements of type comment this method does not work.

Is there a way to find a node by name if there are comments node in the xml

Thanks,




Answer this question

selecting a child

  • Patrick Huynh

    Dear Ori,

    What is this single node's name

    What is the xml file

    What is the XPath expression

    What do you think this (not yet shown) XPath expression should select

    You'd have long ago had the answer to your question in case you had provided this necessary information.

    Cheers,
    Dimitre Novatchev


  • Hoopla

    try this, all you need to do is alter the select statement to suit your needs

    static void Main(string[] args)

    {

    XPathDocument Doc = new XPathDocument("emp.xml");

    XPathNavigator navigator = Doc.CreateNavigator();

    XPathNodeIterator iterator = navigator.Select("/employees/employee");

    while (iterator.MoveNext())

    {

    Console.WriteLine(iterator.Current.Name);

    Console.WriteLine(iterator.Current.Value);

    Console.Read();

    }

    }


  • AlexBB

    How about showing the exact XPath expression you are using

    With the XPath expression

    XML/Child[@name = 'firstChild']

    this C# sample using your XML as the input finds the element without problems:

    string xml = @"<XML>

    <!--Comment-->

    <Child name=""firstChild""/>

    <!--Comment-->

    <Child name=""secondChild""/>

    </XML>
    ";

    XmlDocument xmlDocument = new XmlDocument();
    xmlDocument.LoadXml(xml);

    XmlElement element = xmlDocument.SelectSingleNode(

    "XML/Child[@name = 'firstChild']") as XmlElement;
    if (element != null) {
    Console.WriteLine("Found element with markup {0}", element.OuterXml);
    }
    else {
    Console.WriteLine("No element found.");
    }



  • trampas

    I can see here many answers, but not to my question.

    I'll give my xml:

    <XML>

    <!--Comment-->

    <Child name="firstChild"/>

    <!--Comment-->

    <Child name="secondChild"/>

    </XML>

    when I use xpath expression to select the child named "firstChild" it doesn't find anything. when I remove the comments - it does work



  • pragati22

    There are different types of nodes e.g. element nodes, attribute nodes, processing instruction nodes which all have names. So you need to be more specific on what kind of node you want to find by its name. If you post a minimal but complete XML where the problem occurs and your XPath expression you pass to SelectSingleNode then it will be easy for us to help. Most failing attempts to use SelectSingleNode (or SelectNodes) are caused by namespace issues where an XmlNamespaceManager solves the problem, comments are unlikely to to be an issue.

  • Jonathan Cran

    You keep us intrigued.

    It's easy to say what's wrong with particular XPath expression then guess what can be in yours that forces it to behave this way.



  • nelson888

    Because comments don't have names they can't interfere with expression that selects nodes by name.



  • ACHawk

    > when I use xpath expression to select the child named "firstChild" it doesn't find anything. when I
    > remove the comments - it does work

    In case you also show the XPath expression used, many people will be able to show the problem in this expression.

    Also, your terminology is deeply misleading -- there is no ' child named "firstChild" ' in the presented xml document. All the "Child" nodes are named "Child". They have different values for their "name" attribute. However, the name of an element is not the value of its "name" attribute, and not all elements may have a "name" attribute in general.

    So, if you want to select "the element named "Child", whose "name" attribute has the value of 'firstChild' ", one XPath expression doing this is:

    /*/Child[@name='firstChild'][1]

    In plain English this says:

    The first "Child" element, whose "name" attribute is equal to the string "firstChild" and which is a child of the top element of the XML document.

    Note that there is no guarantee that "the" element exists as there could be more than one elements with "name" attribute equal to "firstChild". This is why the predicate "[1]" (, which is an abbreviation of "[position() = 1]") has also been specified -- in order to selects the first such element.

    Hope this helped.

    Cheers,
    Dimitre Novatchev


  • selecting a child