6. Learn how to add a link to the table. 2. Add a link to the table.

Source: Internet
Author: User

6. Learn how to add a link to the table. 2. Add a link to the table.

Overview:

Linq to xml (ltx for short) is the xml curd provided by Microsoft Based on the linq technology. compared with System. XML operations in XML are easier. during this time, I used it to upgrade the boss's email system in the company. I have some experiences. I 'd like to summarize it.

Main objects:

1. XDocument: XML document object. The loading method is to load the XML document according to its static method: XDocument xDoc = XDocument. load (@ "** xml Path **")

. Element ("NodeName") obtains the XElement object of the subnode. Generally, the XDoc is the root node.

. Sava ("xmlpath") saves the document to an xml file

XElement xele = xdocTypeDef. Element ("root"). Elements (). Where (p => p. Attribute ("Name"). Value = strTopName). Single ();

2. XElement: Node object

. Element ("NodeName") obtains the XElement object of the subnode.

. Elements () returns the child node set

. Elements ("NodeName") returns a subnode set named "NodeName ".

. Add (param [] obj) can contain multiple nodes or attributes.

. Remove () Delete the node.

. Value node attributes

3. XAttribute: attribute object

 

If you know this, you can operate the XML in combination with linq.

 

 

Below are some source code for adding subnodes and attributes to nodes in recent small projects:

1 public static bool addElement (XElement xeleNode, string addEleType, string strContralName, string strContent) 2 {3 XElement xeleChild = new XElement (addEleType); 4 xeleChild. add (new XAttribute (strContralName, strContent); 5 // determine whether the Sub-control of the sub-control 6 if (xeleNode. elements (). count ()> 0 & xeleNode. elements (). where (p => p. attribute (strContralName ). value = strContent ). count ()> 0) 7 return false; 8 xeleNode. add (xeleChild); 9 10 return true; 11 12} 13 14 public static bool addAttr (XElement xeleNode, Dictionary <string, string> dic) 15 {16 bool flag = true; 17 foreach (KeyValuePair <string, string> pair in dic) 18 {19 if (xeleNode. elements (). where (p => p. attribute ("Name "). value = pair. key ). count ()> 0) 20 {21 flag = false; 22 continue; 23} 24 XElement xeleChild = new XElement ("Attribute"); 25 xeleChild. add (new XAttribute ("Name", pair. key. toString (); 26 xeleChild. value = pair. value. toString (); 27 xeleNode. add (xeleChild); 28} 29 return flag; 30}View Code

Delete and modify nodes

... // Modify node XElement xele = XEleFirstNode. elements (). where (p => p. attribute ("Name "). value = strContralName ). single () as XElement; xele = xele. elements (). where (p => p. attribute ("Name "). value = strAttr ). single () as XElement; xele. value = strAttrDes; xDoc. save (strPath );... // del nodeif (MessageBox. show ("are you sure you want to divide? "," Warning ", MessageBoxButtons. yesNo, MessageBoxIcon. warning) = DialogResult. no) return; foreach (object str in lBAtrributes. selectedItems) // listbox mutiselect {string strAttr = str. toString (); XElement xeleAttr = XEleSecondNode. elements (). where (p => p. attribute ("Name "). value = strAttr ). single () as XElement; xeleAttr. remove ();} xDoc. save (strPath );


A lot of exercises can be mastered.

 

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.