添加到现有XML文件中,我在开始元素标记的末尾放置了正斜杠

时间:2014-07-10 03:00:18

标签: 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>
  <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>
</Users>

但有麻烦......

  Named <UserName-3 /> instead of <UserName-3>   <--- where is the / at the end coming from in the beginning tag?
  Did not indent properly
  No closing tag for this element 

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

    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"
    strId = "3"

    ' Setting these variables above for now but they would actually be coming in as arguments and hence will be dynamically set at runtime.

    Dim xEle As XElement = XElement.Load("MyGoodXMLforadding.xml")
    xEle.Add(New XElement("UserName-" & strId))
    xEle.Add(New XElement("Private-" & strId, New XElement("PrivateFtpAccountId-" & strId, strPrivateFtpAcctId), New XElement("PrivatePassword-" & strId, srPrivatePassword)))
    xEle.Add(New XElement("Public-" & strId, New XElement("PublicFtpAccountId-" & strId, strPublicFtpAcctId), New XElement("PublicPassword-" & strId, "test6")))

    ' Save in place.
    xEle.Save("MyGoodXMLforadding.xml")

我甚至硬编码了这个值:

    xEle.Add(New XElement("UserName-3"))

我仍然得到同样的东西。

...问候丹

1 个答案:

答案 0 :(得分:0)

您应该将<Private-X><Public-X>添加到<UserName-X>而不是代码中的根元素(xEle):

........
Dim username = New XElement("UserName-" & strId)
username.Add(New XElement("Private-" & strId, ....))
username.Add(New XElement("Public-" & strId, ....))
xEle.Add(username)
........