Append an xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dschu012
    New Member
    • Jul 2008
    • 39

    Append an xml file

    I have a xml file that looks as such
    Code:
    <people>
        <person name="a" id"123">
        <person name="z" id"312">
    </people>
    and I want to add a person with the name of "b" but put in inbetween "a" and "z" to keep it alphabetical.
    I was able to add to the bottom so it would look like
    Code:
    <people>
        <person name="a" id"123">
        <person name="z" id"312">
        <person name="b" id"452">
    </people>
    with this code

    Code:
    XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(xml);
                XmlNode node = xmlDoc.SelectSingleNode("/people");
                XmlElement el = xmlDoc.CreateElement("person");
                el.SetAttribute("name", name);
                el.SetAttribute("id", id);      
                node.AppendChild(el);
                xmlDoc.Save(xmlFile);
    So does anyone know how I can modify this to allow me to insert into the middle?
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I think you need to go one step further into the tree and find the XmlNode that goes with <person name="a" id"123">
    And then use InsertAfter()

    Comment

    • dschu012
      New Member
      • Jul 2008
      • 39

      #3
      Originally posted by Plater
      I think you need to go one step further into the tree and find the XmlNode that goes with <person name="a" id"123">
      And then use InsertAfter()
      Tried to do that and insert after doesn't accept an XmlElement

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        It inherits from it, type-cast it.

        Code:
        Inheritance Hierarchy:
        System.Object 
           System.Xml.XmlNode 
             System.Xml.XmlLinkedNode 
              System.Xml.XmlElement

        Comment

        • dschu012
          New Member
          • Jul 2008
          • 39

          #5
          Woot thanks for the help. If anyone wants my finished code this is what it looked like
          Code:
          XmlDocument xmlDoc = new XmlDocument();
                      xmlDoc.Load(xml);
                      XmlNodeList nodes = xmlDoc.SelectNodes("/people/person");
                      foreach (XmlNode node in nodes)
                      {
                          XmlAttributeCollection attCol = node.Attributes;
                          if (attCol[0].Value.ToString().CompareTo(newName) >= 0)
                          {
                              XmlElement el = xmlDoc.CreateElement("person");
                              el.SetAttribute("name",newName);
                              el.SetAttribute("id",id);
                              xmlDoc.SelectSingleNode("/people").InsertBefore((XmlNode)el, node);
                              break;
                          }
          
                      }
                      xmlDoc.Save(xmlFile);

          Comment

          Working...