2015-10-06 103 views
0

这里是XML文件和我的代码。我需要更改<Value>标记中的数字2509。下面的代码找到<Value>SchoolFiles</Value>,但之后跳出循环。我知道有一个更好的方式循环。如何使用VB.NET编辑XML文件

XML文件

<Filters> 
    <Filter> 
     <Name>ViewFolders</Name> 
     <ApplyToFiles>0</ApplyToFiles> 
     <ApplyToDirs>1</ApplyToDirs> 
     <MatchType>None</MatchType> 
     <MatchCase>0</MatchCase> 
     <Conditions> 
      <Condition> 
       <Type>0</Type> 
       <Condition>0</Condition> 
       <Value>SchoolFiles</Value> 
      </Condition> 
      <Condition> 
       <Type>0</Type> 
       <Condition>0</Condition> 
       <Value>DataImportFiles</Value> 
      </Condition> 
      <Condition> 
       <Type>0</Type> 
       <Condition>0</Condition> 
       <Value>2509</Value> 
      </Condition> 
     </Conditions> 
    </Filter> 
</Filters> 
Dim FileZillaXMLFilterFile As String = "C:\Users\Development\AppData\Roaming\FileZilla\filters.xml" 
Dim myXmlDocument As XmlDocument = New XmlDocument() 
myXmlDocument.Load(FileZillaXMLFilterFile) 
Dim MyNode As XmlNode 
Dim node As XmlNode 
node = myXmlDocument.SelectSingleNode("//Filters") 
'node = myXmlDocument.SelectSingleNode("//Filters/Filter/Conditions/Condition/Value") 
For Each book As XmlNode In node.ChildNodes 

    Debug.Print(book.InnerText) '= Session("iCustID").ToString() 
    If Microsoft.VisualBasic.Left(book.InnerText, 11) = "ViewFolders" Then 
     For Each node4 As XmlNode In book.ChildNodes 
      If node4.Name = "Conditions" Then 
       'Debug.Print(node4.LastChild.Name) 

       For Each node5 As XmlNode In node4.ChildNodes 
        Debug.Print(node5.Name) 
        For Each node6 As XmlNode In node5.ChildNodes 
         Debug.Print(node6.Name) 
         If node6.Name = "Value" Then 
          Debug.Print(node6.InnerText) 
          For Each node7 As XmlNode In node6.ChildNodes 
           If node7.InnerText <> "SchoolFiles" And node7.InnerText <> "DataImportFiles" Then 
            'need to change 2509 to another numnber 
            Debug.Print(node7.InnerText) 
           End If 
          Next 
         End If 
        Next 
       Next 
      End If 
     Next 
    End If 
Next 
+1

更改xml中的任何值都需要重写整个xml文件。 – nelek

+0

https://msdn.microsoft.com/en-us/library/system.xml.xmlnode.selectsinglenode(v=vs.110).aspx –

回答

1

诀窍这样做是使用XPath找到合适的节点,然后改变它的价值。然后,您可以使用XmlDocument重写该文件。所需要的节点的XPath是:

/Filters/Filter/Conditions/Condition[Value != 'SchoolFiles' and Value != 'DataImportFiles']/Value 

在这里,我复制你的检查SchoolFiles和DataImportFiles的逻辑,但可能有更好的方法来集中在正确的节点上。例如,它可能基于它的索引。或者如果类型节点具有唯一值,则可以使用该值。

我在你的代码中看到你已经在使用SelectSingleNode。你可以使用这个XPath来节省大量的循环。您可以将节点的Value属性设置为新数字,并使用Save方法保存XMLDocument。

像XMLSpy这样的工具在获取XPath正确方面很有用。 W3Schools(http://www.w3schools.com/xml/)是学习XML和XPath的好地方。

+0

这个.XML文件的问题之一是它有重复标签,我刚刚显示了它的一部分。所以你的例子找到了最上面的部分。这就是我沿着循环路径走下去的原因。我真的不想在这里粘贴整个文件,我也没有看到我可以上传任何文件的位置? –

+0

好吧,我得到它太工作,使用我原来的代码和你的建议保存...如果node7.InnerText <>“SchoolFiles”和node7.InnerText <>“DataImportFiles”然后 '需要更改2509到另一个numnber 调试。打印(node7.InnerText) node7.Value =“2509” myXmlDocument.Save(FileZillaXMLFilterFile) –