0
XML에 익숙하지 않으며 기존 XML 파일에 새 노드를 추가하는 데 문제가 있습니다.VB.net에서 기존 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.")
감사 ... 이것은 하드 코딩으로 작동합니다 v alues하지만 동적 인 태그가 런타임시와 innertext에서 빌드되도록해야합니다. 나는 그 예에서 대부분을 할 수있었습니다. – user3020047
@ user3020047 - 게시물을 수정하여 동적으로 작동하는 방식을 보여줍니다. – dbasnett