¿Cómo modificar el valor de nodo, mientras que la iteración con XPathNodeIterator?
-
01-10-2019 - |
Pregunta
Estoy navegando documento XML con XPathNodeIterator, y quiero cambiar los valores de algunos nodos. No puedo encontrar la manera: (
Aquí está el código que estoy usando:
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?
}
}
Y la muestra archivo XML:
<?xml version="1.0" encoding="utf-8"?>
<foo>
<bar>
<item>
<Param />
<SubitemsList>
<name>name one</name>
<name>name two</name>
...
</SubitemsList>
</item>
...
</bar>
</foo>
O tal vez hay una mejor manera de hacer esto?
Solución
He encontrado una manera:
- Reemplazar
XPathDocument
conXmlDocument
- Cuando llegue al nodo necesaria
...
XmlNode node = ((IHasXmlNode)nodesIterator.Current).GetNode();
node.InnerText = "new text";
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow