简体   繁体   中英

xml loop to last node and insert c#

i'm looping through xml and i want to insert data after the last node.

this is the xml and the code i have so far

any help would be great

        <User Name="">
        <Option Name="Pass"></Option>
        <Option Name="Group"></Option>
        <Option Name="Bypass server userlimit">0</Option>
        <Option Name="User Limit">0</Option>
        <Option Name="IP Limit">0</Option>
        <Option Name="Enabled">1</Option>
        <Option Name="Comments"></Option>
        <Option Name="ForceSsl">0</Option>
        <IpFilter>
            <Disallowed />
            <Allowed />
        </IpFilter>
        <Permissions>
            <Permission Dir="kk"
                <Option Name="AutoCreate">0</Option>
            </Permission>
        </Permissions>
    </User>
</Users>

 XmlDocument xdcDocument = new XmlDocument();

    xdcDocument.Load(@"C:\Users\Rob\Desktop\filezilla.xml");


    XmlElement xelRoot = xdcDocument.DocumentElement;
    XmlNodeList xnlNodes = xelRoot.SelectNodes("/User");

    foreach (XmlNode xNode in xnlNodes)
    {
        //how do i loop to the user node and then insert afterwards?
    }

First your XML must be valid, I have added 2 fixes to your XML, you would get the exception telling you where exactly the error is.

    <Users>

       <User Name="">
        <Option Name="Pass"></Option>
        <Option Name="Group"></Option>
        <Option Name="Bypass server userlimit">0</Option>
        <Option Name="User Limit">0</Option>
        <Option Name="IP Limit">0</Option>
        <Option Name="Enabled">1</Option>
        <Option Name="Comments"></Option>
        <Option Name="ForceSsl">0</Option>
        <IpFilter>
            <Disallowed />
            <Allowed />
        </IpFilter>
        <Permissions>
            <Permission Dir="kk">
                <Option Name="AutoCreate">0</Option>
            </Permission>
        </Permissions>
    </User>
</Users>

The code that would do what you are asking for:

 XmlDocument xdcDocument = new XmlDocument();

                xdcDocument.Load("filezilla.xml");

                XmlElement xelRoot = xdcDocument.DocumentElement;

                // select the last node of type User
                XmlNodeList xmlNodes = xelRoot.SelectNodes("/Users/User[last()]");


                if (xmlNodes.Count == 1)
                {
                    var node = xmlNodes[0];

                    // create your new node, whatever structure you want
                    var element = xdcDocument.CreateElement("NodeAfterLastUser");

                    // insert this node to the same parent after the last user
                    node.ParentNode.InsertAfter(element, node);

                    xdcDocument.Save("filezilla_result.xml");
                }

Result:

<Users>
  <User Name="">
    <Option Name="Pass">
    </Option>
    <Option Name="Group">
    </Option>
    <Option Name="Bypass server userlimit">0</Option>
    <Option Name="User Limit">0</Option>
    <Option Name="IP Limit">0</Option>
    <Option Name="Enabled">1</Option>
    <Option Name="Comments">
    </Option>
    <Option Name="ForceSsl">0</Option>
    <IpFilter>
      <Disallowed />
      <Allowed />
    </IpFilter>
    <Permissions>
      <Permission Dir="kk">
        <Option Name="AutoCreate">0</Option>
      </Permission>
    </Permissions>
  </User>
  <NodeAfterLastUser />
</Users>

Key notes: the selection of last user node made by XPath syntax: /Users/User[last()] You can find here more examples and explanation about selections: http://www.w3schools.com/xpath/xpath_syntax.asp

Then just add the element that you want, I created in my example element with name NodeAfterLastUser for illlustration, but here where your node should be actually placed.

Good luck.

Your xml is not valid. After fixing it as:

<Users>
       <User Name="">
        <Option Name="Pass"></Option>
        <Option Name="Group"></Option>
        <Option Name="Bypass server userlimit">0</Option>
        <Option Name="User Limit">0</Option>
        <Option Name="IP Limit">0</Option>
        <Option Name="Enabled">1</Option>
        <Option Name="Comments"></Option>
        <Option Name="ForceSsl">0</Option>
        <IpFilter>
            <Disallowed />
            <Allowed />
        </IpFilter>
        <Permissions>
            <Permission Dir="kk">
                <Option Name="AutoCreate">0</Option>
            </Permission>
        </Permissions>
    </User>
</Users>

You can use Linq2Xml

var xDoc = XDocument.Load(filename);
xDoc.XPathSelectElement("/Users") //Find parent Node
    .Add( new XElement("NewNode","NewValue")); //Add to parent as its last child
var newxml = xDoc.ToString();

The output is:

<Users>
  <User Name="">
    <Option Name="Pass"></Option>
    <Option Name="Group"></Option>
    <Option Name="Bypass server userlimit">0</Option>
    <Option Name="User Limit">0</Option>
    <Option Name="IP Limit">0</Option>
    <Option Name="Enabled">1</Option>
    <Option Name="Comments"></Option>
    <Option Name="ForceSsl">0</Option>
    <IpFilter>
      <Disallowed />
      <Allowed />
    </IpFilter>
    <Permissions>
      <Permission Dir="kk">
        <Option Name="AutoCreate">0</Option>
      </Permission>
    </Permissions>
  </User>
  <NewNode>NewValue</NewNode>
</Users>

PS: Don't forget to include the namespaces

using System.Xml.XPath;
using System.Xml.Linq;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM