namespacexml{classProgram {Static voidMain (string[] args) { stringPath =@"C:\Users\Administrator\Desktop\xml\study.xml"; //reading an XML fileXDocument Xdoc =xdocument.load (path); //Get and NodeXElement root =Xdoc. Root; //Traversing child nodes foreach(varItem1inchRoot. Elements ()) {foreach(varItem2inchitem1. Elements ()) {Console.WriteLine (item2. Name+"========="+item2. Value); }} console.readkey (); } }}
namespacexml{classProgram {Static voidMain (string[] args) { //Write XML fileXDocument Xdoc =NewXDocument (); //Creating the root nodeXElement root =NewXElement (" Person"); XElement Student=NewXElement ("Student"); Student. Setattributevalue ("Stuid","1"); Student. Setelementvalue ("name","Small Balls"); Student. Setelementvalue ("Gender","female"); Student. Setelementvalue (" Age"," -"); Root. ADD (student); Xdoc. ADD (root); //SaveXdoc. Save (@"C:\Users\Administrator\Desktop\xml\1.xml"); Console.WriteLine ("Create success!"); Console.readkey (); } }}
C # advanced------XML