如何在VB.net中添加到现有的XML文件?

时间:2014-07-09 22:28:34

标签: xml vb.net

XML新手,无法在现有XML文件中添加新节点。

以下是XML文件的外观:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Users>
  <UserName-1>
    <Private-1>
      <PrivateFtpAccountId-1>11111</PrivateFtpAccountId-1>
      <PrivatePassword-1>test1</PrivatePassword-1>
    </Private-1>
    <Public-1>
      <PublicFtpAccountId-1>22222</PublicFtpAccountId-1>
      <PublicPassword-1>test2</PublicPassword-1>
    </Public-1>
  </UserName-1>
  <UserName-2>
    <Private-2>
      <PrivateFtpAccountId-2>33333</PrivateFtpAccountId-2>
      <PrivatePassword-2>test3</PrivatePassword-2>
    </Private-2>
    <Public-2>
      <PublicFtpAccountId-2>44444</PublicFtpAccountId-2>
      <PublicPassword-2>test4</PublicPassword-2>
    </Public-2>
  </UserName-2>
</Users>

我想在最后一次分组后添加这些内容。

<UserName-3>
    <Private-3>
      <PrivateFtpAccountId-3>55555</PrivateFtpAccountId-3>
      <PrivatePassword-3>test5</PrivatePassword-3>
    </Private-3>
    <Public-3>
      <PublicFtpAccountId-3>66666</PublicFtpAccountId-3>
      <PublicPassword-3>test6</PublicPassword-3>
    </Public-3>
</UserName-3>

在运行我的代码后,我生成了这个XML文件(我正在替换原文):

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Users>
  <UserName-1>
    <Private-1>
      <PrivateFtpAccountId-1>11111</PrivateFtpAccountId-1>
      <PrivatePassword-1>test1</PrivatePassword-1>
    </Private-1>
    <Public-1>
      <PublicFtpAccountId-1>22222</PublicFtpAccountId-1>
      <PublicPassword-1>test2</PublicPassword-1>
    </Public-1>
  </UserName-1>
  <UserName-2>
    <Private-2>
      <PrivateFtpAccountId-2>33333</PrivateFtpAccountId-2>
      <PrivatePassword-2>test3</PrivatePassword-2>
    </Private-2>
    <Public-2>
      <PublicFtpAccountId-2>44444</PublicFtpAccountId-2>
      <PublicPassword-2>test4</PublicPassword-2>
    </Public-2>
  </UserName-2>
  <Private-3>
    <PrivateFtpAccountId-3>55555</PrivateFtpAccountId-3>
    <PublicFtpAccountId-3>test5</PublicFtpAccountId-3>
  </Private-3>
  <Public-3>
    <PublicFtpAccountId-3>66666</PublicFtpAccountId-3>
    <PublicFtpAccountId-3>test6</PublicFtpAccountId-3>
  </Public-3>
</Users>

但无法获取用户名标签,而其他标签则介于两者之间 如上所示。我尝试了一些没有成功的事情。那些“事物”不再出现在代码中了。

我在控制台应用中的代码:

    Dim strPrivateRoot As XmlNode
    Dim strPublicRoot As XmlNode
    Dim strUserNameRoot As XmlNode
    Dim strElementPrivateFtpAcctId As XmlNode
    Dim strElementPrivatePassword As XmlNode
    Dim strElementPublicFtpAcctId As XmlNode
    Dim strElementPublicPassword As XmlNode
    Dim strId As String
    Dim strPrivateFtpAcctId As String
    Dim strPrivatePassword As String
    Dim strPublicFtpAcctId As String
    Dim strPublicPassword As String

    strPrivateFtpAcctId = "55555"
    strPrivatePassword = "test5"
    strPublicFtpAcctId = "66666"
    strPublicPassword = "test6"

    ' Can be any Id.
    strId = "3"

    ' Create a new XmlDocument class, and use the Load method to load the XML file.
    Dim myXmlDocument As New XmlDocument()

    '' The XmlDocument class represents the XML document and has a Load method to load the document from a file, stream, or an XmlReader.
    '' So load in the XML file.
    myXmlDocument.Load("MyGoodXMLforadding.xml")

    ' Grab the root to start adding after.
    Dim objRoot = myXmlDocument.DocumentElement

    ' For Private:
    strPrivateRoot = myXmlDocument.CreateElement("Private-" & strId)

    strElementPrivateFtpAcctId = myXmlDocument.CreateElement("PrivateFtpAccountId-" & strId)
    strElementPrivateFtpAcctId.InnerText = strPrivateFtpAcctId
    strPrivateRoot.AppendChild(strElementPrivateFtpAcctId)

    strElementPrivatePassword = myXmlDocument.CreateElement("PublicFtpAccountId-" & strId)
    strElementPrivatePassword.InnerText = strPrivatePassword
    strPrivateRoot.AppendChild(strElementPrivatePassword)

    myXmlDocument.DocumentElement.AppendChild(strPrivateRoot)

    ' For Public:
    strPublicRoot = myXmlDocument.CreateElement("Public-" & strId)

    strElementPublicFtpAcctId = myXmlDocument.CreateElement("PublicFtpAccountId-" & strId)
    strElementPublicFtpAcctId.InnerText = strPublicFtpAcctId
    strPublicRoot.AppendChild(strElementPublicFtpAcctId)

    strElementPublicPassword = myXmlDocument.CreateElement("PublicFtpAccountId-" & strId)
    strElementPublicPassword.InnerText = strPublicPassword
    strPublicRoot.AppendChild(strElementPublicPassword)

    myXmlDocument.DocumentElement.AppendChild(strPublicRoot)

    ' Save in place.
    myXmlDocument.Save("MyGoodXMLforadding.xml")
    Console.WriteLine("The XML file was saved successfully.")

1 个答案:

答案 0 :(得分:1)

XElement具有读写文件的方法。

加载代码后

'orig = XElement.Load("FILENAMES_HERE")
Dim orig As XElement =
  <Users>
    <UserName-1>
      <Private-1>
        <PrivateFtpAccountId-1>11111</PrivateFtpAccountId-1>
        <PrivatePassword-1>test1</PrivatePassword-1>
      </Private-1>
      <Public-1>
        <PublicFtpAccountId-1>22222</PublicFtpAccountId-1>
        <PublicPassword-1>test2</PublicPassword-1>
      </Public-1>
    </UserName-1>
    <UserName-2>
      <Private-2>
        <PrivateFtpAccountId-2>33333</PrivateFtpAccountId-2>
        <PrivatePassword-2>test3</PrivatePassword-2>
      </Private-2>
      <Public-2>
        <PublicFtpAccountId-2>44444</PublicFtpAccountId-2>
        <PublicPassword-2>test4</PublicPassword-2>
      </Public-2>
    </UserName-2>
  </Users>

Dim newxml As XElement =
  <UserName-3>
    <Private-3>
      <PrivateFtpAccountId-3>55555</PrivateFtpAccountId-3>
      <PrivatePassword-3>test5</PrivatePassword-3>
    </Private-3>
    <Public-3>
      <PublicFtpAccountId-3>66666</PublicFtpAccountId-3>
      <PublicPassword-3>test6</PublicPassword-3>
    </Public-3>
  </UserName-3>

orig.Add(newxml)
'orig.Save("FILENAMES_HERE")

编辑 - 关于如何在运行时构建它的一些想法。

    Dim theUsers As XElement = <users></users>

    ' theUsers=XElement.Load("FILE_NAME")

    Dim someIDs() As String = New String() {"1", "2", "3", "4"}
    Dim sampleUsers() As String = New String() {"lorem", "ipsum", "dolor", "sit"}
    Dim prvtIDs() As String = New String() {"11", "12", "13", "14"}
    Dim prvtPass() As String = New String() {"p1", "p2", "p3", "p4"}
    Dim pubIDs() As String = New String() {"21", "22", "23", "24"}
    Dim pubPass() As String = New String() {"pu1", "pu2", "pu3", "pu4"}

    'add many
    For x As Integer = 0 To someIDs.Length - 1
        Dim aUser As XElement = <username uid=<%= someIDs(x) %>>
                                    <name><%= sampleUsers(x) %></name>
                                    <private>
                                        <ftpAcct>
                                            <id><%= prvtIDs(x) %></id>
                                            <password><%= prvtPass(x) %></password>
                                        </ftpAcct>
                                    </private>
                                    <public>
                                        <ftpAcct>
                                            <id><%= pubIDs(x) %></id>
                                            <password><%= pubPass(x) %></password>
                                        </ftpAcct>
                                    </public>
                                </username>
        theUsers.Add(aUser)
    Next

    'add one user
    Dim anIDs As String = "9999"
    Dim aUsernm As String = "1user"
    Dim prvtID As String = "199"
    Dim prvtPasswd As String = "prvtPass"
    Dim pubID As String = "299"
    Dim pubPasswd As String = "pubPass"

    theUsers.Add(<username uid=<%= anIDs %>>
                     <name><%= aUsernm %></name>
                     <private>
                         <ftpAcct>
                             <id><%= prvtID %></id>
                             <password><%= prvtPasswd %></password>
                         </ftpAcct>
                     </private>
                     <public>
                         <ftpAcct>
                             <id><%= pubID %></id>
                             <password><%= pubPasswd %></password>
                         </ftpAcct>
                     </public>
                 </username>)

    ' theUsers.Save("FILE_NAME")

我删除了aaaa-number业务,以便像这样的东西工作

    'find a user with an id of 3
    Dim foo As IEnumerable(Of XElement) = From bar In theUsers.Elements
                                          Where bar.@uid = "3" Select bar Take 1


    'or find user 'lorem'
    foo = From bar In theUsers.Elements
          Where bar...<name>.Value = "lorem" Select bar Take 1

以下是作为函数的概念

Public Function Adduser(anID As String,
                        aUsernm As String,
                        prvtID As String,
                        prvtPassWD As String,
                        pubID As String,
                        pubPassWD As String) As XElement

    Return <username uid=<%= anID %>>
               <name><%= aUsernm %></name>
               <private>
                   <ftpAcct>
                       <id><%= prvtID %></id>
                       <password><%= prvtPassWD %></password>
                   </ftpAcct>
               </private>
               <public>
                   <ftpAcct>
                       <id><%= pubID %></id>
                       <password><%= pubPassWD %></password>
                   </ftpAcct>
               </public>
           </username>
End Function

它会像这样被称为

    theUsers.Add(Adduser("42", "user42", "id42", "pp42", "pubid42", "pubpass42"))