We know that W3C has a DOM model and C # has a class library that operates XML under the DOM model. However, after the emergence of LINQ, Microsoft re-built a set of XML models, and the operations were the same as those of the DOM model, but they were simpler.
The following figure shows the model:
The above is a new class library. The core class is the xelement. It is definitely the core class, but it should not be regarded as a low level. Some other features are different from the DOM model. One of them is that xattribute and xnode are at the same level, and xdocument is no longer necessary. For other differences, see the DOM model for comparison.
The following code compares the differences between the DOM model and the LINQ model in operating XML:
// Dom Model
Xmldocument Doc = new xmldocument ();
Xmlelement name = Doc. createelement ("name ");
Name. innertext = "Patrick Hines ";
Xmlelement phone1 = Doc. createelement ("phone ");
Phone1.setattribute ("type", "home ");
Phone1.innertext = "206-555-0144 ";
Xmlelement phone2 = Doc. createelement ("phone ");
Phone2.setattribute ("type", "work ");
Phone2.innertext = "425-555-0145 ";
Xmlelement street1 = Doc. createelement ("street1 ");
Street1.innertext = "123 Main St ";
Xmlelement city = Doc. createelement ("city ");
City. innertext = "Mercer Island ";
Xmlelement state = Doc. createelement ("state ");
State. innertext = "wa ";
Xmlelement postal = Doc. createelement ("Postal ");
Postal. innertext = "68042 ";
Xmlelement address = Doc. createelement ("Address ");
Address. appendchild (street1 );
Address. appendchild (city );
Address. appendchild (State );
Address. appendchild (Postal );
Xmlelement contact = Doc. createelement ("Contact ");
Contact. appendchild (name );
Contact. appendchild (phone1 );
Contact. appendchild (phone2 );
Contact. appendchild (Address );
Xmlelement contacts = Doc. createelement ("contacts ");
Contacts. appendchild (contact );
Doc. appendchild (contacts );
// LINQ Model
Xelement contacts =
New xelement ("contacts ",
New xelement ("Contact ",
New xelement ("name", "Patrick Hines "),
New xelement ("phone", "206-555-0144 ",
New xattribute ("type", "home ")),
New xelement ("phone", "425-555-0145 ",
New xattribute ("type", "work ")),
New xelement ("Address ",
New xelement ("street1", "123 Main St "),
New xelement ("city", "Mercer Island "),
New xelement ("State", "wa "),
New xelement ("Postal", "68042 ")
)
)
);
From the comparison, we can also see the simplicity of the LINQ model. We can also see the importance of xelement from the LINQ model. You can use xelement to not only create XML files from the beginning, but also load the files. You can also retrieve the required elements from the database, which requires the use of the content of the LINQ to SQL statement. You can also retrieve the elements from the array. After the operation is complete, you can save it using the Save method.
The following describes how to add, delete, query, and modify XML.
// Query
Foreach (C in contacts. nodes ())...{
Console. writeline (C );
}
We can see that each element does not need to be forcibly converted when outputting XML elements. Here, the C # compiler has done these tasks, it calls the tostring () method of each element during output.
// Insert element
Xelement mobilephone = new xelement ("phone", "206-555-0168 ");
Contact. Add (mobilephone );
Here is just a simple demonstration of some operations. For those complex operations, as long as the DOM model can implement the LINQ model, it will be able to implement. You can also use addafterthis and addbeforethis to improve efficiency during insertion.
// Delete an element
Contact. element ("phone"). Remove (); // delete a specific element
Contact. Elements ("phone"). Remove (); // delete a group of elements
Contacts. element ("Contact"). element ("Address"). removecontent (); // Delete the content of an element
The setelement method can also be used to delete an element. Setting an element to null deletes the element.
// Modify the element
Contact. element ("phone"). replacecontent ("425-555-0155"); // modify the content of the first phone element.
Of course, you can also use the setelement method. This is where it is used.
From the above brief introduction, we can clearly see how simple it is to operate XML using LINQ. Here, the C # syntax is used. If VB. NET is used, it will be simpler. Some methods can be used in VB. NET, but not in C. After all, VB. NET is a late binding language and can give full play to its advantages.