是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"))我还是得到了同样的东西。
Regards...Dan
发布于 2021-09-08 12:48:32
在XML中,此表示法表示元素为空,它是一个自动结束标记:
<UserName-3 /> 它在逻辑上类似于下面的空元素,但是空元素的语法要长得多:
<UserName-3></UserName-3>具有自动完成和语法突出显示功能的XML编辑器最初会将您的元素创建为自动关闭,因为它在您定义属性时保持XML有效,这优化了用户体验,但也意味着您不太可能忘记关闭标签。
发布于 2014-07-10 11:41:08
您应该将<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)
........https://stackoverflow.com/questions/24666798
复制相似问题