C # basic consolidation (3)-How To read XML from Linq To XML,
Record some methods for reading XML, so that you do not forget it when using it. You have to spend time searching it.
I. Reading XML in traditional writing
Now I have an XML file as follows:
Now I want to find the Id and sex (gender) and age (age) of the person named "Wang Wu)
Code:
Static void Main (string [] args) {XDocument xdoc = XDocument. load ("4.xml"); // Load the xml List <XElement> eles = new List <XElement> (); // obtain the node set XElement ele = xdoc that meets the conditions. element ("root"); // obtain the root node string nameTxt = ""; // The condition TraditionalReadXML (ele, eles, nameTxt) to be searched ); // find the data that meets the conditions and add it to the eles set. The foreach (XElement item in eles) // traverses the data and outputs {Console. writeLine ("Id: {0}, name: {1}, sex: {2}, age: {3}", item. attribute ("id "). value, item. element ("name "). value, item. element ("sex "). value, item. element ("age "). value);} Console. readKey ();} /// <summary> /// traditionally read XML /// </summary> /// <param name = "ele"> find the data under which node </param> /// <param name = "eles"> set to store consistent data </param> /// <param name = "nameTxt"> name to be searched </param> private static void TraditionalReadXML (XElement ele, list <XElement> eles, string nameTxt) {// first, traverse all the subnodes under ele. Note that this is a subnode. You cannot find the subnode foreach (XElement item in ele. 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. value = nameTxt) {// The node that meets the condition, take the parent eles. add (item. parent) ;}} // recursive TraditionalReadXML (item, eles, nameTxt) if there are child nodes in the item );}}
The result of code execution is:
Ii. Read XML using the Linq syntax
Change the above Code:
Static void Main (string [] args) {XDocument xdoc = XDocument. load ("4.xml"); // Load the xml List <XElement> eles = new List <XElement> (); // obtain the string nameTxt =" "set of qualified nodes "; // The condition to be searched // obtain the node that meets the conditionVar query = from s in xdoc. Descendants () where s. Name. LocalName = "name" & s. Value = nameTxt select s. Parent;Foreach (XElement item in query) // Add qualified nodes to the eles set {eles. add (item);} foreach (XElement item in eles) // traverses the Retrieved Data and outputs {Console. writeLine ("Id: {0}, name: {1}, sex: {2}, age: {3}", item. attribute ("id "). value, item. element ("name "). value, item. element ("sex "). value, item. element ("age "). value);} Console. readKey ();
}
The bold lines are written in linq, and their implementation is the same, which is much simpler than normal writing.
Iii. Writing lambda statements for Linq Query
Change the code:
Static void Main (string [] args) {XDocument xdoc = XDocument. load ("4.xml"); // Load the xml List <XElement> eles = new List <XElement> (); // obtain the string nameTxt =" "set of qualified nodes "; // The condition to be searched // traverse the node and add matching conditions to the eles setForeach (XElement I in xdoc. descendants (). where (e => {if (e. name. localName = "name") {if (e. value = nameTxt) {return true ;}} return false ;})) {eles. add (I. parent );}Foreach (XElement item in eles) // traverses the Retrieved Data and outputs {Console. writeLine ("Id: {0}, name: {1}, sex: {2}, age: {3}", item. attribute ("id "). value, item. element ("name "). value, item. element ("sex "). value, item. element ("age "). value);} Console. readKey ();}
The above are three methods for reading XML, which can be used according to your favorite method.