如何使用XmlDocument查找元素然后更新innertext?使用vb.net

时间:2014-07-15 14:21:43

标签: xml vb.net

无法遍历XML文档以找到某个元素,以便我可以 然后更新innertext。

我的xml看起来像:

<?xml version="1.0" encoding="utf-8"?>
<FileZillaServer>
  <Groups />
  <Users>
    <User Name="jbrown">
      <Option Name="Pass">2ac9cb7dc02b3c0083eb70898e549b63</Option>
      <Option Name="ForceSsl">0</Option>
      <Option Name="8plus3">0</Option>
      <IpFilter>
        <Disallowed />
        <Allowed />
      </IpFilter>
      <Permissions>
        <Permission Dir="C:\inetpub\wwwroot">
          <Option Name="FileRead">1</Option>
          <Option Name="DirList">1</Option>
          <Option Name="DirSubdirs">1</Option>
          <Option Name="AutoCreate">0</Option>
        </Permission>
      </Permissions>
      <SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="0" UlType="0" UlLimit="10" ServerUlLimitBypass="0">
        <Download />
        <Upload />
      </SpeedLimits>
    </User>
    <User Name="3-Private">
      <Option Name="Pass">test5</Option>
      <Option Name="ForceSsl">0</Option>
      <Option Name="8plus3">0</Option>
      <IPFilter>
        <Disallowed />
        <Allowed />
      </IPFilter>
      <Permissions>
        <Permission Dir="C:\Backup Spaces\3\files">
          <Option Name="FileRead">1</Option>
          <Option Name="DirList">1</Option>
          <Option Name="AutoCreate">0</Option>
        </Permission>
      </Permissions>
      <SpeedLimits ServerUlLimitBypass="0" UlLimit="10" UlType="0" ServerDlLimitBypass="0" DlLimit="10" DlType="0">
        <Download />
        <Upload />
      </SpeedLimits>
    </User>
    <User Name="3-Public">
      <Option Name="Pass">test6</Option>
      <Option Name="ForceSsl">0</Option>
      <Option Name="8plus3">0</Option>
      <IPFilter>
        <Disallowed />
        <Allowed />
      </IPFilter>
      <Permissions>
         <Permission Dir="C:\Backup Spaces\3\files">
         <Option Name="FileRead">1</Option>
         <Option Name="DirList">1</Option>
         <Option Name="AutoCreate">0</Option>
      </Permission>
      </Permissions>
      <SpeedLimits ServerUlLimitBypass="0" UlLimit="10" UlType="0" ServerDlLimitBypass="0" DlLimit="10" DlType="0">
        <Download />
        <Upload />
      </SpeedLimits>
    </User>
  </Users>
  <Settings>
    <Item name="Serverports" type="string">21</Item>
    <Item name="Number of Threads" type="numeric">2</Item>
  <SpeedLimits>
     <Download />
     <Upload />
   </SpeedLimits>
 </Settings>

我正在尝试找到User Name =“3-Private”,然后在其下面输入Name =“Pass”,这样我就可以将密码值从test5更改为test10。

我的控制台应用代码是:

    Dim xmlNode2 As XmlNode
    Dim strId As String
    Dim strElementName As String
    Dim strCurrentInnerTextValue As String
    Dim strNewValue As String
    Dim bSuccess As Boolean = False

    Dim xmlNode As XmlNode

    Dim myXmlDocument As XmlDocument = New XmlDocument()

    myXmlDocument.Load("MyFileZillaforupdating.xml")

    ' Use the XmlNode object that the DocumentElement property of the XmlDocument returns to manipulate an XML node.
    xmlNode = myXmlDocument.DocumentElement

    strId = "3"

    ' Search field.
    strElementName = strId & "-Private"


    ' ----> My attempt at searching but having touble getting to the element I want.

    For Each xmlNode In xmlNode.ChildNodes
        'Find the child node.
        For Each xmlNode2 In xmlNode.ChildNodes
            If xmlNode2.Name = strElementName Then
                strCurrentInnerTextValue = xmlNode2.InnerText

                ' Going to update the password.
                strNewValue = "test10"

                Console.WriteLine("Current Innertext Value = " & strCurrentInnerTextValue & "   New Innertext value = " & strNewValue)
                ' Pause.
                Console.ReadLine()

                ' Update the XML to the new value.
                xmlNode2.InnerText = strNewValue

                bSuccess = True
            End If
        Next
    Next

    If bSuccess = True Then
        myXmlDocument.Save("MyFileZillaforupdating.xml")
        Console.WriteLine("The XML file was saved successfully.")
    Else
        Console.WriteLine("The entry was not found.")
    End If

1 个答案:

答案 0 :(得分:0)

您可以使用LINQ to XML查找密码元素,然后进行更新。

Dim passwordElement =
    (From u In myXmlDocument...<User>
    Where u.@Name = "3-Private"
    From o In u.<Option>
    Where o.@Name = "Pass"
    Select o).FirstOrDefault()
If passwordElement IsNot Nothing Then
    passwordElement.Value = "test10"
End If

首先找到名称=&#34; 3-Private&#34;的User元素,然后在这些元素中找到名称=&#34的Option元素;传递&#34;,然后选择第一场比赛(如果有的话)。

您需要使用System.Xml.Linq.XDocument代替XmlDocument,但它也有Load方法。