2014-07-09 2 views
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.") 

답변

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") 

편집 될 것이다로드. 이런 그 물건이 여기에

'find a user with an id of 3 
    Dim foo As IEnumerable(Of XElement) = From bar In theUsers.Elements 
              Where [email protected] = "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 

같은 개념 일 수 있도록

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 번호 사업을 제거

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

감사 ... 이것은 하드 코딩으로 작동합니다 v alues하지만 동적 인 태그가 런타임시와 innertext에서 빌드되도록해야합니다. 나는 그 예에서 대부분을 할 수있었습니다. – user3020047

+0

@ user3020047 - 게시물을 수정하여 동적으로 작동하는 방식을 보여줍니다. – dbasnett

관련 문제