Person.xml
<?XML version= "1.0" encoding= "Utf-8"?><MyP> <P1> < PersonID= "cz001"> <Name>Tom</Name> < Age>18</ Age> </ Person> < PersonID= "cz002"> <Name>John doe</Name> < Age>19</ Age> </ Person> < PersonID= "cz003"> <Name>Harry</Name> < Age>20</ Age> </ Person> </P1> <P2> < Person> <Name>Zhang 32</Name> < Age>18</ Age> </ Person> < Person> <Name>Lee 42</Name> < Age>19</ Age> </ Person> < Person> <Name>Wang 52</Name> < Age>20</ Age> </ Person> </P2></MyP>
View Code
usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespaceXML Operation { Public Partial classForm1:form { PublicForm1 () {InitializeComponent (); } Private voidButton1_Click (Objectsender, EventArgs e) { //Read XMLSystem.Xml.Linq.XDocument Xdoc = System.Xml.Linq.XDocument.Load ("Person.xml"); System.Xml.Linq.XElement Xeroot= Xdoc. Root;//root nodeSystem.Xml.Linq.XElement Xele = xeroot.element ("P1"). Element (" Person");//child NodesMessageBox.Show ("id="+ Xele. Attribute ("ID"). Value);//cz001 foreach(varEleinchXele. Elements ()) {stringstr =string. Format ("Name={0},value={1}", Ele. Name.tostring (), Ele. Value); MessageBox.Show (str); } } Private voidForm1_Load (Objectsender, EventArgs e) { //* Read XML data loaded into the TreeView1 list. //1. Load Person.xmlSystem.Xml.Linq.XDocument Xdoc = System.Xml.Linq.XDocument.Load ("Person.xml"); //2. Get root node, MyPSystem.Xml.Linq.XElement Xeroot =Xdoc. Root; //3. Add the root node to the TreeView1 firstTreeNode Treeviewroot =TreeView1.Nodes.Add (xeRoot.Name.LocalName); //4. Start traversing all child nodes, using recursionloadtreenodes (Xeroot, treeviewroot); //* Write XML file. //1. Creating an XML ObjectSystem.Xml.Linq.XDocument XDocument =NewSystem.Xml.Linq.XDocument (); //2. Create a node withSystem.Xml.Linq.XElement Eroot =NewSystem.Xml.Linq.XElement ("root node"); //Add to XdocXDocument. ADD (Eroot); //3. Add child nodesSystem.Xml.Linq.XElement Ele1 =NewSystem.Xml.Linq.XElement ("child Node 1"); Ele1. Value="Content 1"; Eroot.add (ELE1); //4. Adding properties for ELE1 nodesSystem.Xml.Linq.XAttribute attr =NewSystem.Xml.Linq.XAttribute ("URL","http://www.baidu.com"); Ele1. ADD (attr); //5. Quick Add child node methodEroot.setelementvalue ("child Node 2","Content 2"); //6. Quickly add attributesEle1. Setattributevalue ("ID", A); //7, last saved to a file, can also be written to the stream. XDocument. Save ("123.xml"); } Private voidloadtreenodes (System.Xml.Linq.XElement xeroot, TreeNode treeviewroot) {//loop all the content below the Xeroot on the TreeView foreach(System.Xml.Linq.XElement eleinchxeroot.elements ()) { //determine if there are child nodes under the Child root node if(Ele. Elements (). Count () >0) {TreeNode node= TreeViewRoot.Nodes.Add (ele. Name.tostring ());//Write Name Value//Get property methods on a nodeSystem.Xml.Linq.XAttribute attr = ele. Attribute ("ID"); if(attr! =NULL) {node. Text+=string. Format ("[{0}={1}]", attr. Name, attr. Value); } loadtreenodes (ele, node); //Traversal Loops } Else{treeViewRoot.Nodes.Add (ele). Value); //write value under the node } } } }}
C # Xml Linq XDocument Basic operations