2014-07-14 2 views
1

XmlDocument 방식을 사용하는 다른 노드 다음에 노드를 추가 할 수 없습니다. 내가 XMLdocument의 방법을 시도했지만 그들은 내게 을 줄 것 같지 않습니다. 나는 아래이 새로운 노드 섹션을 추가 내 콘솔 응용 프로그램 코드의 코드를 실행노드를 기존 XML 문서에 삽입하는 동안 노드를 추가하려면 어떻게해야합니까?

<?xml version="1.0" encoding="UTF-8"?> 
<FileZillaServer> 
<Groups/> 
<Users> 
    <User Name="dbrown"> 
     <Option Name="Pass">2ac9cb7dc02b3c0083eb70898e549b63</Option> 
     <IpFilter> 
      <Disallowed/> 
      <Allowed/> 
     </IpFilter> 
     <Permissions> 
      <Permission Dir="C:\inetpub\wwwroot"> 
       <Option Name="FileRead">1</Option> 
      </Permission> 
     </Permissions> 
     <SpeedLimits DlType="0" DlLimit="10" ServerDlLimitBypass="0" UlType="0" UlLimit="10" ServerUlLimitBypass="0"> 
      <Download/> 
      <Upload/> 
     </SpeedLimits> 
    </User> 
</Users> 
<User Name="3"> 
    <Option Name="Pass"/> 
    <IPFilter> 
     <Disallowed/> 
     <Allowed/> 
    </IPFilter> 
    <Permissions> 
     <Permission Dir="C:\inetpub\wwwroot"> 
      <Option Name="FileRead"/> 
     </Permission> 
    </Permissions> 
    <SpeedLimits DType="0" DLimit="10" ServerDLimitBypass="0" UType="0" ULimit="10" ServerULimitBypass="0"> 
     <Download/> 
     <Upload/> 
    </SpeedLimits> 
    <PrivateFtpAccountId-3>55555</PrivateFtpAccountId-3> 
    <PrivatePassword-3>test5</PrivatePassword-3> 
    <PublicFtpAccountId-3>66666</PublicFtpAccountId-3> 
    <PublicPassword-3>test6</PublicPassword-3> 
    </User> 
    </FileZillaServer> 

,하지만 난 그냥 처음 사용자 그룹 후에 추가 할 ---- : 같은

기존의 XML 파일이 보이는 > ....

<User Name="3"> 
    <Option Name="Pass"/> 
    <IPFilter> 
     <Disallowed/> 
     <Allowed/> 
    </IPFilter> 
    <Permissions> 
     <Permission Dir="C:\inetpub\wwwroot"> 
      <Option Name="FileRead"/> 
     </Permission> 
    </Permissions> 
    <SpeedLimits DType="0" DLimit="10" ServerDLimitBypass="0" UType="0" ULimit="10" ServerULimitBypass="0"> 
     <Download/> 
     <Upload/> 
    </SpeedLimits> 
    <PrivateFtpAccountId-3>55555</PrivateFtpAccountId-3> 
    <PrivatePassword-3>test5</PrivatePassword-3> 
    <PublicFtpAccountId-3>66666</PublicFtpAccountId-3> 
    <PublicPassword-3>test6</PublicPassword-3> 
    </User> 

작동하지 않는 것 같습니다.

내 콘솔 응용 프로그램 코드는 다음과 같습니다

Dim xmleUserNameRoot As XmlElement 
    Dim xmleOptionElement10 As XmlElement 
    Dim xmleOptionElement20 As XmlElement 
    Dim xmleOptionElement21 As XmlElement 
    Dim xmleOptionElement22 As XmlElement 
    Dim xmleOptionElement23 As XmlElement 

    Dim xmleIpFilterRoot As XmlElement 
    Dim xmlePermissionsGroup As XmlElement 
    Dim xmlePermissionRoot As XmlElement 
    Dim xmleSpeedLimits As XmlElement 

    Dim xmleElementPrivateFtpAcctId As XmlElement 
    Dim xmleElementPrivatePassword As XmlElement 
    Dim xmleElementPublicFtpAcctId As XmlElement 
    Dim xmleElementPublicPassword As XmlElement 

    Dim count As Integer 
    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("MyFiLeZillaforadding.xml") 

    ' For starting User Name. 
    xmleUserNameRoot = myXmlDocument.CreateElement("User") 

    ' Create an attribute and set its value to that of the new id. 
    xmleUserNameRoot.SetAttribute("Name", strId) 
    myXmlDocument.DocumentElement.AppendChild(xmleUserNameRoot) 

    ' Create a new element with an attribute and add it in to the User group. 
    xmleOptionElement1 = myXmlDocument.CreateElement("Option") 
    xmleOptionElement1.SetAttribute("Name", "Pass") 
    xmleUserNameRoot.AppendChild(xmleOptionElement1) 

    ' Create the new "IPFilter" root. 
    xmleIpFilterRoot = myXmlDocument.CreateElement("IPFilter") 

    ' Add elements to the "IPFilter" root. 
    xmleOptionElement20 = myXmlDocument.CreateElement("Disallowed") 
    xmleIpFilterRoot.AppendChild(xmleOptionElement20) 

    xmleOptionElement21 = myXmlDocument.CreateElement("Allowed") 
    xmleIpFilterRoot.AppendChild(xmleOptionElement21) 

    ' Add the "IPFilter" root. 
    xmleUserNameRoot.AppendChild(xmleIpFilterRoot) 

    ' Create the new "Permissions" group. 
    xmlePermissionsGroup = myXmlDocument.CreateElement("Permissions") 

    ' Create the new "Permission" root. 
    xmlePermissionRoot = myXmlDocument.CreateElement("Permission") 
    xmlePermissionRoot.SetAttribute("Dir", "C:\inetpub\wwwroot") 

    ' Add elements to the "Permission" root. 
    xmleOptionElement10 = myXmlDocument.CreateElement("Option") 
    xmleOptionElement10.SetAttribute("Name", "FileRead") 
    xmlePermissionRoot.AppendChild(xmleOptionElement10) 

    ' Add the "Permission" root to the "Permissions" group. 
    xmlePermissionsGroup.AppendChild(xmlePermissionRoot) 

    ' Add the "Permissions" group. 
    xmleUserNameRoot.AppendChild(xmlePermissionsGroup) 

    ' Create the new "SpeedLimits" root. 
    xmleSpeedLimits = myXmlDocument.CreateElement("SpeedLimits") 

    xmleSpeedLimits.SetAttribute("ServerULimitBypass", "0") 
    xmleSpeedLimits.SetAttribute("ULimit", "10") 
    xmleSpeedLimits.SetAttribute("UType", "0") 
    xmleSpeedLimits.SetAttribute("ServerDLimitBypass", "0") 
    xmleSpeedLimits.SetAttribute("DLimit", "10") 
    xmleSpeedLimits.SetAttribute("DType", "0") 

    ' Add elements to the "SpeedLimits" root. 
    xmleOptionElement22 = myXmlDocument.CreateElement("Download") 
    xmleSpeedLimits.AppendChild(xmleOptionElement22) 

    xmleOptionElement23 = myXmlDocument.CreateElement("Upload") 
    xmleSpeedLimits.AppendChild(xmleOptionElement23) 

    ' Add the "SpeedLimits" root. 
    xmleUserNameRoot.AppendChild(xmleSpeedLimits) 

    ' Add the remaining to User Name="#". 
    xmleElementPrivateFtpAcctId = myXmlDocument.CreateElement("PrivateFtpAccountId-" & strId) 
    xmleElementPrivateFtpAcctId.InnerText = strPrivateFtpAcctId 
    xmleUserNameRoot.AppendChild(xmleElementPrivateFtpAcctId) 

    xmleElementPrivatePassword = myXmlDocument.CreateElement("PrivatePassword-" & strId) 
    xmleElementPrivatePassword.InnerText = strPrivatePassword 
    xmleUserNameRoot.AppendChild(xmleElementPrivatePassword) 

    xmleElementPublicFtpAcctId = myXmlDocument.CreateElement("PublicFtpAccountId-" & strId) 
    xmleElementPublicFtpAcctId.InnerText = strPublicFtpAcctId 
    xmleUserNameRoot.AppendChild(xmleElementPublicFtpAcctId) 

    xmleElementPublicPassword = myXmlDocument.CreateElement("PublicPassword-" & strId) 
    xmleElementPublicPassword.InnerText = strPublicPassword 
    xmleUserNameRoot.AppendChild(xmleElementPublicPassword) 

    ' Get the first User. 
    ' ----> Can't see to get the first user. I tried a number of the XmlDocument methods with no sucess. 


    ' Add the whole User Name="#" group after this first User. 


    ' Save in place. 
    myXmlDocument.Save("MyFiLeZillaforadding.xml") 

    Console.WriteLine("The XML file was saved successfully.") 

감사합니다 ...

당신이 문서에 추가하고자하는 새로운 데이터의 '사용자'노드를 만드는 것 같습니다

답변

1

,

xmleUserNameRoot.SetAttribute("Name", strId) 
myXmlDocument.DocumentElement.AppendChild(xmleUserNameRoot) 

당신이 다음 기존 문서의 Users 노드를 찾기 수행하고 필요하면 새를 추가하고 여기 문서의 말미에 추가그 노드.

자식 노드를 얻은 다음 Users.appendChild 인 것을 찾으십시오.

documentation on the XmlDocumentClass이 도움이됩니다.

편집

그래서 XML 파일의 구조를 알고, 그리고 Users 노드는 문서의 마지막 자식 노드의 자식 노드 것을,이 코드로 해당 노드에 액세스 할 수 있었다 :

또한
Dim docNodes As XmlNodeList = myXmlDocument.LastChild.ChildNodes() 

    For Each node As XmlNode In docNodes 
     If node.Name = "Users" Then 
      node.AppendChild(xmleUserNameRoot) 
     End If 
    Next 

Users 노드가 차례로 문서의 마지막 노드 인 FileZillaServer 노드의 마지막 노드 인 것을 알고 있기 때문에, 당신은 단순히

을 할 수
myXmlDocument.LastChild.LastChild.AppendChild(xmleUserNameRoot) 

매우 유연하지는 않지만 작동합니다!

+0

'첫 번째 사용자를 확보하십시오. '----> 첫 번째 사용자를 확보 할 수 없습니다. XmlDocument 메서드를 성공적으로 시도했습니다. '첫 번째 사용자 다음에 전체 User Name = "#"그룹을 추가하십시오. – user3020047

+0

곧 Enter 키를 누르십시오. 하하. 어쨌든 .. 그 AppendChild 코드는 거기에 속하지 않습니다. User 노드를 찾으면 맨 아래에 속합니다. 내가 가지고있는 문제는 사용자 childnode를 찾는 대부분의 방법을 시도했지만 성공하지 못했다는 것입니다. 내가 뭘 잘못하고 있었는지 확실하지 않았다 ... 그리고 기본적으로 토론을 위해 그 메소드 호출 시도를 삭제했다. – user3020047

+0

당신이하려는 일을 정확하게 이해한다면 사용자 자식 노드를 찾을 필요가 없습니다. 당신이 필요로하는 것은'Users' 부모 노드를 찾고 그 끝에 새로운 노드를 추가하는 것입니다.이 경우 새로운 노드가 첫 번째 기존 사용자 노드 옆에 추가됩니다. – Anthony

0

이 줄이 당신이 후 삽입 할 노드를 삽입해야 당신이 여기

Dim xmlNodeToInsert As XmlNode 

를 삽입 할 노드를 정의 xmlNodeFirstUser

Dim xmlNodeFirstUser As XmlNode = myXmlDocument.SelectSingleNode("//User") 

에 myXmlDocument 내에서 최초의 사용자 요소를 선택합니다 xml 문서의 첫 번째 사용자 요소

myXmlDocument.InsertAfter(xmlNodeToInsert, xmlNodeFirstUser) 

내에 XmlDocument 방법 조합 사용.Net 인 SelectSingleNode(string xpathExpression)InsertAfter(XmlNode toInsert, XmlNode insertAfterMe)을 사용하면 쉽게 XML 문서를 조작하고 트래버스 할 수 있습니다. Please look here 원하는 노드를 선택하려면 XPath 표현식을 조사해야합니다.

+0

제안대로 노력했지만 얻을 수 있습니다. 처리되지 않은 예외 : System.ArgumentException : 참조 노드가이 노드의 자식 이 아닙니다. 나는 다음과 같이 추가했다 : Dim xmlNodeFirstUser As XmlNode = myXmlDocument.SelectSingleNode ("// User") 그리고 그쪽은 User 노드를 리턴한다. 나는 추가 : myXmlDocument.InsertAfter (xmleUserNameRoot, xmlNodeFirstUser) 첫 번째 arguementis defeind로 XmlElement 그게 내가 사용하고 있습니다. 나는 그것이 실패하고있는 이유라고 생각한다. – user3020047

+0

XmlElement 대신 XmlNode를 사용하는 것 외에도'myXmlDocument.DocumentElement.InsertAfter()'를 사용해보십시오. 편집 - 일반적으로 참조 노드가이 노드의 자식이 아니므로 xmlNoeFirstUser가 myXmlDocument의 자식 노드가 아닙니다. – Tijol

관련 문제