Some Applications of c # linq

Source: Internet
Author: User
Tags abstract definition

I recently learned xml. I have also found some materials on the Net, which are not comprehensive. So I have written something here to share with you. Due to my limited knowledge, if there is an error, please refer to the Extensible Markup Language, A subset of Standard Generic Markup languages, a markup language used to mark electronic files so that they are structured. It can be used to mark data and define data types. It is a source language that allows users to define their own markup language. It is ideal for World Wide Web transmission and provides a unified way to describe and exchange structured data independent of applications or vendors. (Baidu) to learn xml. linq, first we need to understand several definitions of xml. 1. XDeclaration ----- the XML declaration is used to declare whether the XML version, encoding, and XML document are independent, if you want to create an xml file, you must declare some xml Information at the beginning of the xml file. 1 XDeclaration m_XDexlaration = new XDeclaration ("1.0", "UTF-8", "yes "); // initialize XDeclaration2 XDocument m_Xdocument = new XDocument (m_XDexlaration, new XElement ("person ")); // initialize XDocument. Remember to have a root node when initializing a document. Otherwise, the error "3 m_Xdocument.Save (" person. xml "); // The result of saving the document: <? Xml version = "1.0" encoding = "UTF-8" standalone = "yes"?> <Person/> 2. XElement refers to the markup language containing the start and end labels in angle brackets. For example, <item> person </item> and item is an element 3. XAttribute attribute. For example, color in <item color = "red"> person </item> is the attribute of item 4. A node is a relatively abstract definition. in xml documents, a node can be an element or something else. xml. use of some of the methods of linq 1. initialize an xml document XDeclaration xs = new XDeclaration ("1.0", "UTF-8", "yes"); XDocument xdoc = new XDocument (xs, new XElement ("person ", new XAttribute ("color", "red"), new XAttribute (" Size "," big "), new XElement (" name "," Liu Qing "), new XElement (" age "," 18 "); xdoc. save ("p. xml "); Console. writeLine (xdoc); pay attention to the following points during document initialization: 1. first, XDeclaration should be included, that is, the xml definition, such as the version, and the encoding method is 2. there must be a root node when initializing xml. For example, the person above is a root node. The preceding two attributes of the root node are defined: color and size. There are two subnodes named and age under person. <? Xml version = "1.0" encoding = "UTF-8" standalone = "yes"?> <Person color = "red" size = "big"> <name> Liu Qing </name> <age> 18 </age> </person> 2. xml file traversal, for example, when there is an xml file <? Xml version = "1.0" encoding = "UTF-8"?> <Menu> <strip menu = "file"> <item name = "file" shortcut key = "alt + o"> open a workspace </item> <item name = "file" shortcut Key = "alt + p"> close workspace </item> <item name = "file" shortcut key = "alt + q"> Save workspace </item> <item name = "file" shortcut key = "alt + m"> withdraw from workspace </item> <item name = "file" shortcut key = "alt + n"> Print </item> </strip> <strip menu = "edit"> <item name = "edit"> copy </item> <item name = "edit"> cut </item> <item name = "edit"> paste </item> <item name = "edit"> Menu manager </item> </strip> </Menu> now I want to traverse all its items, and Copy code 1 ArrayList name = new ArrayList (); 2 ArrayList s_name; 3 XDocument m_Xdocument = XDocument. load ("MenuXml. XML "); // use the static method of the xdocument class to read the document 4 XElement m_Xlement = m_Xdocument.Element (" Menu "); // read the root element 5 IEnumerable <XElement> sdf = m_Xlement.Elements ("strip"); // read the nodes under the strip Element (there are two strip node elements and several item elements) 6 int I = 0; 7 foreach (XElement sld in sdf) 8 {9 name. add (sld. attribute ("menu "). V Alue); // obtain the strip attribute menu10 Console. writeLine (name [I ++]); 11} 12 IEnumerable <XElement> I _XElment = m_Xlement.Elements (); // obtain the root element (Menu) all node elements below 13 foreach (XElement x in I _XElment) 14 {15 IEnumerable <XElement> p_xelment = x. elements ("item"); 16 s_name = new ArrayList (); 17 foreach (XElement e in p_xelment) 18 {19 20 Console. write (e. value. toString (); 21} 22 23 24} copy code file edit open workspace close workspace save workspace exit workspace print copy cut Click any key in the cut-and-paste menu manager to continue... 3. delete a specific node. Assume that an xml document is MenuXml and its content is the copy code. <? Xml version = "1.0" encoding = "UTF-8"?> <Menu> <strip menu = "file"> <item name = "file" shortcut key = "alt + p"> close a workspace </item> <item name = "file" shortcut Key = "alt + q"> Save workspace </item> <item name = "file" shortcut key = "alt + m"> exit workspace </item> <item name = "file" shortcut key = "alt + n"> Print </item> </strip> </Menu> copy the code. Now I want to delete one of the elements named "Close workspace" copy code 1 XDocument m_Xdocument = XDocument. load ("MenuXml. XML "); // use the static method of the xdocument class to read the document 2 XElement m_Xlement = m_Xdocument.Element (" Menu "); // read the root element 3 4 IEnumerable <XElement> xe = m_Xlement.Elements (); // obtain the set 5 foreach (XElement x in xe) under the root element) 6 {7 IEnumerable <XElement> xes = x. elements ("item"); // obtain the set of items 8 foreach (XElement ds in xes) 9 {10 if (ds. value. toString (). trim () = "Close workspace") // compare 11 {12 ds. remove (); 13 m_Xdocument.Save ("MenuXml1.XML"); // save as the new xml14} 15} 16} 17 XDocument a1 = XDocument. load ("MenuXml. XML "); 18 XDocument a2 = XDocument. load ("MenuXml1.XML"); 19 Console. writeLine (a1); 20 Console. writeLine (a2 ); the result of copying the code is as follows: <Menu> <strip menu = "file"> <item name = "file" shortcut key = "alt + p"> close workspace </item> <item name = "file" shortcut key = "alt + q"> Save workspace </item> <item name = "file" shortcut key = "alt + m"> exit workspace </item> <item name = "file" shortcut key = "alt + n"> Print </item> </strip> </Menu> <strip menu = "file"> <item name = "file" shortcut key = "alt + q"> Save workspace </item> <item name = "file" shortcut key = "alt + m"> exit Workspace </item> <item name = "file" shortcut key = "alt + n"> Print </item> </strip> </Menu> press any key to continue... when learning linq xml, pay attention to the following methods. element () only obtains the first group of tags (if there are multiple pairs of tags)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.