LINQ to XML is a LINQ-enabled memory XML programming interface that you can use to work with XML in the. NET Framework programming language.
It places the XML document in memory, much like the Document Object Model (DOM). You can query and modify an XML document, modify it, save it as a file, or serialize it and send it over the network. However, LINQ to XML is different from DOM: it provides a new object model, a lighter model, and more convenient to use, which leverages the language improvements of visualc# 2008.
The most important advantage of LINQ to XML is its integration with language-integrated Query (LINQ). Because this integration is implemented, queries can be written to the memory XML document to retrieve a collection of elements and attributes. The query functionality of LINQ to XML is functionally, albeit not syntactically, comparable to XPath and XQuery. After Visual C # 2008 is integrated into LINQ, it provides stronger typing capabilities, compile-time checking, and improved debugger support.
Using the query results as parameters of the XElement and XAttribute object constructors, a powerful way to create an XML tree is implemented. This approach, called "function construction," enables developers to easily convert an XML tree from one shape to another.
LINQ to XML provides an improved XML programming interface, which may be as important as the LINQ functionality of LINQ to XML. With LINQ to XML, when you program XML, you can implement any expected actions, including:
- Loads XML from a file or stream.
- Serializes XML to a file or stream.
- Create XML from scratch using function constructs.
- Query XML using an XPath-like axis.
- Use the ADD, Remove, ReplaceWith, and SetValue methods to manipulate the memory XML tree.
- Use XSD to validate the XML tree.
Using a combination of these features, you can convert an XML tree from one shape to another.
Instance
In the Nurse station project that I developed in the previous period, I was the For loop XmlReader way to traverse the XML file. can also complete the relevant requirements, and the function is encapsulated well, can be called directly, but also very convenient.
The following is presented in LINQ to XML. Specific more detailed things can be checked msnd.
The client's XML file is as follows, I have only taken 3 items for ease of presentation:
?
| 123456789101112131415161718192021222324252627282930 |
<?xml version="1.0"?><ROWSET> <ROW> <就诊序号>1</就诊序号> <就诊日期>2012-06-11</就诊日期> <病人ID>00002468</病人ID> <姓名>吴建平</姓名> <性别>男</性别> <年龄>42</年龄> <就诊科室>2981</就诊科室> </ROW> <ROW> <就诊序号>2</就诊序号> <就诊日期>2012-06-11</就诊日期> <病人ID>00002467</病人ID> <姓名>蔡蕊</姓名> <性别>女</性别> <年龄>33</年龄> <就诊科室>2981</就诊科室> </ROW> <ROW> <就诊序号>3</就诊序号> <就诊日期>2012-06-11</就诊日期> <病人ID>412905</病人ID> <姓名>aaa</姓名> <性别>男</性别> <年龄>24</年龄> <就诊科室>2981</就诊科室> </ROW></ROWSET> |
How do you show it? Follow the Form1_Load method as follows:
?
| 12345678910111213141516171819202122232425262728 |
using System;using System.Linq;using System.Windows.Forms;using System.Xml.Linq;namespace Lint2XMLDatabinding{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { string xmlFilePath = @"./当前在科病人基本信息.xml"; XDocument doc = XDocument.Load(xmlFilePath); var query = from p in doc.Descendants("ROW")//doc.Descendants("ROW") where p.Element("就诊序号").Value!="1" select new { A = p.Element("就诊序号").Value, B = p.Element("病人ID").Value, C = p.Element("姓名").Value ,D=p.Element("性别").Value }; dataGridView1.DataSource= query.ToList(); } }} |
The program results are as follows:
The demo used in this post is simple enough to use LINQ. There are many ways to manipulate an XML file, without LINQ, of course, with no problem.
In fact, so far, the. NET framework has provided two classes of libraries for working with XML data:
First, the DEBUGLZQ in front of the project in accordance with the DOM Standard Class library: Treasury XmlDocument, XmlElement, XmlNode, XmlAttribute and other classes. This class library is characterized by its high-performance, powerful, but also burdened with too much XML tradition and complexity.
Second, LINQ-based class library: including XDocument, XElement, XNode, XAttribute and other classes. This class library is characterized by the ability to query and manipulate LINQ for quick and easy use.
Using LINQ to XML in a C # program to query an instance of XML-formatted data