Refactor Code

  • Gravatar
    Query XML using XPathDocument and XPathNodeIterator

    by vipraIT on 7/22/2008 10:49:45 AM
  • I try to query XML to retreive a particular element value but it gives complete XML document instead of particular element or node. XML document is like- Autobiography of Bebo Dev Robin 999.99
  • private void QueryXml()
    {
    XPathDocument document = new XPathDocument("\\books.xml");
    XPathNavigator navigator = document.CreateNavigator();

    // Select all books authored by Robin
    XPathNodeIterator nodes = navigator.Select("descendant::book [author/last-name='Robin']");
    while (nodes.MoveNext())
    {
    Clone the navigator returned by the Current property.
    // Use the cloned navigator to get the title element.
    XPathNavigator clone = nodes.Current.Clone();
    clone.MoveToFirstChild();

    lblXml.Text = clone.Value;
    }
    }
    }
  • Refactor it!
Please log in to refactor the code! Login