A preliminary understanding of linq to xml and a preliminary understanding of linqtoxml
I recently learned about lint to xml and wrote a blog here, so that I can view it later ~~
1. Frequently Used classes
XmlDocument -- document (xml file)
XmlElement -- Element (node) XmlAttribute -- attribute (node content) XNamespace -- namespace (xmlns) XDeclaration -- "1.0", "UTF-8" is generated by default when an XML file is created ", "yes" 2. Create an xml file (Save)
Private static void Xml () {XDocument xml = new XDocument (); // The description automatically creates an Encoding for the UTF-8 XDeclaration dec = new XDeclaration ("1.0", "UTF-8 ", "yes"); XNamespace xmln = "SB-GAVIN-AA"; // Add an xml namespace, xmlns XNamespace xm = "GAVIN-AA"; XElement eleHead = new XElement (xmln + "Head "); // create a node and declare that the namespace of the node is xmln XElement eleDetail = new XElement ("name"); // create a node. If the namespace has been declared before, does not inherit by default. xmlns = "" XElement ele = new XElement (xm + "column") is added. // create a node, the namespace of each node can be different from XAttribute appearance = new XAttribute ("appearance", "nice"); // you can create an attribute XAttribute name = new XAttribute ("name ", "Hao"); // create an xml property. add (eleHead); // Add the root node eleHead. add (eleDetail); // Add the sub-node eleDetail. add (ele); // Add the sub-node eleDetail. add (appearance); // Add the attribute xml to the child node. save ("3.xml"); // Save the file and Save it in the Debug directory}
<?xml version="1.0" encoding="utf-8"?><Head xmlns="SB-GAVIN-AA"> <name appearance="nice" xmlns=""> <column xmlns="GAVIN-AA" /> </name></Head>
3. XML reading
Private static void LoadXml () {XDocument xdoc = XDocument. load (@".. \ Debug \ xml \ 3.xml ");//.. -------> ..... \ bin \ List <XElement> ele = new List <XElement> (); string Apprearance = "nice"; XNamespace xmln = "SB-GAVIN-AA"; XNamespace xm = "GAVIN-AA "; XElement e = xdoc. element (xmln + "Head"); // when the Element contains xmlns, add XNamespace to locate the node. XNamespace foreach (XElement item in e. elements () {// determine whether the element name is name. if it is name, check whether the content is nameTxt if (item. name. localName = "name") {if (item. attribute ("appearance "). value = Apprearance) {// The node that meets the condition, and its parent level ele. add (item. parent) ;}}foreach (XElement item in ele) {Console. writeLine ("appearance: {0}", item. element ("name "). attribute ("appearance "). value); // output result: appearance: nice} Console. readKey ();}
4. Delete and modify XML nodes
(Delete) original xml file:
<?xml version="1.0" encoding="utf-8"?><Head> <name appearance="nice"> <column name="Hao" /> </name> <column /></Head>
Private static void RemoveXml () {XDocument xdoc = XDocument. load (@".. \ Debug \ xml \ 4.xml"); XElement e = xdoc. element ("Head"); // locate the node Head foreach (XElement xxElement in e. elements () {if (xxElement. name. localName = "column") {xxElement. remove (); // Delete the subnode column xdoc in the root node Head. save (@".. \ Debug \ xml \ 4.xml"); // save changes} Console. readKey ();}}
After deletion:
<?xml version="1.0" encoding="utf-8"?><Head> <name appearance="nice"> <column name="Hao" /> </name></Head>
(Change)
Private static void updateXml () {XDocument xdoc = XDocument. load (@".. \ Debug \ xml \ 4.xml"); XElement e = xdoc. element ("Head"); string att = "supnice"; string name = "Hao"; foreach (XElement xxElement in e. elements () {if (xxElement. name. localName = "name") {xxElement. name = name; // modify the node Name xxElement. attribute ("appearance "). value = att; // modify the appearance attribute to supnice xdoc. save (@".. \ Debug \ xml \ 4.xml"); // save changes }}}