2010-09-09 50 views
1

我正在使用XPathNodeIterator導航XML文檔,並且我想更改一些節點的值。 我無法弄清楚如何:(如何在使用XPathNodeIterator迭代時修改節點值?

下面是我使用的代碼:

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? 
    } 
} 

和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> 

或者,也許有更好的方法來做到?此

+0

請不要把標籤的稱號。我已經爲你刪除了它們。 – Robaticus 2010-09-09 17:07:43

+0

@Robaticus,thnx,對不起 – flamey 2010-09-09 17:15:00

+0

不用擔心。改變並不需要很多努力。 – Robaticus 2010-09-09 17:18:33

回答

0

我找到了一種方法:

  1. XmlDocument
  2. 更換XPathDocument當我到所需要的節點

...

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