Manipulating XML tree -- adding, deleting, modifying

1. Add elements to the XML tree

  Add() , add content at the end of the current child node

  AddFirst() , add content before the first child node

  AddAfterSelf() , add content after the current node

  AddBeforeSelf() , add content before the current node

2. Remove elements and attributes from the XML tree

 RemoveAll() , removes all attributes and child elements

 RemoveAttributes(), remove all attributes

 SetAttributeValue(), add, delete or modify the value of the specified attribute

 SetElementValue(), add, delete or modify the value of the specified child element

 

3. Supplement, XNode.Remove(), removes the current node from the parent node,

             Extensions.Remove() , removes all nodes in the collection from their parent nodes.

 static  void AddElement() {
 //             // Create a simple xml data
 //             string xmlData = @"<Root>
 //                                <ItemList1>
 //                                <Item1 />
 //                                <Item2 />
 //                                </ItemList1>
 / /                               ";
 //             // Write to a temporary file
 //             File.WriteAllText("tmpData.xml", xmlData);
 //             XElement root = XElement.Load("tmpData.xml",LoadOptions.SetLineInfo); 
            XElement root = XElement.Load("F:\\XML\\siren.XML");
            XElement ele1 = root.Element("body");
            XElement ele0 = new XElement("ItemList0");
            ele0.Add(new XElement("Item02"));
            ele0.AddFirst( new XElement( " Item01 " )); // Add data before the first node of the current node 
            ele0.Add( new XElement( " Item03 " ), new XElement( " Item04 " ));
            ele1.AddBeforeSelf(ele0); // Used to add content before the current node 
            XElement ele4 = new XElement( " ItemList4 " );
            IEnumerable<XElement> elelist = from e in ele1.Elements()
                                            select e;
            ele4.AddFirst(elelist);
            ele1.AddAfterSelf(ele4);
            ele1.AddAfterSelf( new XElement( " ItemList2 " ), new XElement( " ItemList3 " )); // Add content after the current node
             // Console.WriteLine(root);

            // root.RemoveAttributes(); // Remove all attributes, but not including the attributes of child elements, and the outermost attribute
             // root.RemoveAll();          // Remove all child elements and attributes
             // root.RemoveNodes();          // Remove all child nodes 
            XElement ele = root.Element( " body " ); // Select a node and add an attribute to a node 
            ele.SetAttributeValue( " age " , " 23 " );
            ele.SetElementValue( " Name " , null ); // Remove the value of a single child element, remove the first one in case of multiple 
            ele.SetElementValue( " Name1 " , " 330 " ); // Add child elements .XElement     
            ele2 = ele.Element( " Name1 " );
            ele2.SetAttributeValue("name", "校长");
            ele2.SetAttributeValue( " Age " , " 56 " );
             var lit0 = from xl in root. Elements( " ItemList0 " ) // Query to a node set 
                       select xl;
             // foreach (var item in lit0)
             // {
             //     Console.WriteLine(item);
             // } 
            lit0.Remove(); // Remove this node from the parent element 
            XElement ele11 = root.Element( " ItemList2 " );// Remove a single node, no matter who the parent node is 
            ele11.Remove();
            
            Console.WriteLine(root);
            

        }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324705343&siteId=291194637