views:

38

answers:

1

I'm navigating XML document with XPathNodeIterator, and I want to change some nodes' values. I can't figure out how :(

Here's the code I'm using:

XPathDocument docNav = new XPathDocument(path);

XPathNavigator nav = docNav.CreateNavigator();
nav.MoveToRoot();

XPathNodeIterator itemsIterator = nav.Select("/foo/bar/item");
while (mediumsIterator.MoveNext())
{
    XPathNodeIterator subitemsIterator = itemsIterator.Current.Select("SubitemsList/name");
    while (subitemsIterator.MoveNext())
    {
        XPathNodeIterator nodesIterator = itemsIterator.Current.Select("Param");
        nodesIterator.MoveNext();
        String the_params = nodesIterator.Current.Value;

        // check if I need to modify nodesIterator.Current.Value
        // ...
        // ok I do - how?
    }
}

And the XML file sample:

<?xml version="1.0" encoding="utf-8"?>
<foo>
  <bar>
    <item>
      <Param />
      <SubitemsList>
        <name>name one</name>
        <name>name two</name>
        ...
      </SubitemsList>
    </item>
    ...
  </bar>
</foo>

Or maybe there's a better way to do this?

A: 

I found a way:

  1. Replace XPathDocument with XmlDocument
  2. When I get to the needed node

...

XmlNode node = ((IHasXmlNode)nodesIterator.Current).GetNode();
node.InnerText = "new text";
flamey