首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在VB.net中添加现有的XML文件?

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

Stack Overflow用户
提问于 2014-07-10 06:28:34
回答 1查看 203关注 0票数 0

是XML的新手,在向现有XML文件添加新节点时遇到问题。

下面是XML文件的外观:

代码语言:javascript
复制
<?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>

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

代码语言:javascript
复制
<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文件(我将替换原来的文件):

代码语言:javascript
复制
<?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>

但是,如上所述,在获取用户名标记和其他标记之间有困难。我尝试了几件事都没有成功。这些“东西”不再存在于代码中。

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

代码语言:javascript
复制
    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.")
EN

回答 1

Stack Overflow用户

发布于 2014-07-10 07:24:20

XElement具有读取和写入文件的方法。

加载后,代码将为

代码语言:javascript
复制
'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")

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

代码语言:javascript
复制
    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业务,这样就可以正常工作了。

代码语言:javascript
复制
    '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

下面是作为函数的概念

代码语言:javascript
复制
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

它的名字是这样的

代码语言:javascript
复制
    theUsers.Add(Adduser("42", "user42", "id42", "pp42", "pubid42", "pubpass42"))
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24664703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档