Use LINQ to XML in C # To access XML files

Source: Internet
Author: User

Using LINQ to XML makes it easy to access XML files. The posts written on the Internet are slightly messy. The one on the blog homepage todayArticleIt is not clear what I wrote. Below I will write down the method of accessing files using LINQ to XML in common, for the reference of the brothers who have not written it:

The first step is to select the copy to out attribute (such as test. XML), select always, this step means that the XML file will always be output to the folder where the executable file is located, so that we can directly access it using a file name without a path. The following describes the content of the XML file:

//////////////////////////////////////// ////////////////////////////////

<? XML version = "1.0" encoding = "UTF-8"?>
<Root>
<! -- First XML format -->
<Firststyle>
<Test> one </test>
<Test> two </test>
<Test> three </test>
</Firststyle>

<! -- The second XML format, which is commonly used in configuration files -->
<Secondstyle>
<Test _ name = "firstname" value = "Xu"/>
<Test _ name = "lastname" value = "Jin"/>
<Test _ name = "lastname" value = "Lin"/>
</Secondstyle>

 

<! -- The third XML format, with the first two -->

<Thirdsytle>
<Test1 name = "test1"> test1_value </test1>
</Thirdsytle>
</Root>
//////////////////////////////////////// //////////////////////////

<Root> </root> only indicates the root node of XML. <firstsytle> </firststyle> defines the first format and can nest nodes infinitely, <Test> is a multi-value nested subnode of <firststyle>. Three "values" have been assigned to the node: One Two Three.Code. <Secondstyle> defines the second format. The <Test _> node does not have a "value", but adds "attribute" Name and value. The attribute name can be customized. We will also explain how to read the values in these attributes. These two methods can be used in combination, that is, the third mode in XML. Without additional instructions, there is no access instance in the following access code. You can refer to the first two methods in combination, let's look at the code below:

 

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. xml;
Using system. xml. LINQ;

Namespace linqxml
{
Class Program
{
Static void main (string [] ARGs)
{
// Read the first XML format
VaR firstcollection = from test in xelement. Load ("test. xml"). element ("firststyle"). Elements ("test ")
Select Test. value;
Foreach (string s in firstcollection)
{
Console. writeline (s );
}

// Read the second XML format
VaR secondcollection = from test in xelement. Load ("test. xml"). element ("secondstyle"). Elements ("test _")
Where test. Attribute ("name"). value. Equals ("lastname ")
Select Test. Attribute ("value"). value;
Foreach (string s in secondcollection)
{
Console. writeline (s );
}

}
}
}

When selecting an XML node, you do not need to select the internal and internal nodes, but use the node name. If this node is a single node (for example, a few style nodes), you should use element ("") for selection. If it is a multi-node (for example, <Test> ), you must use elements (""). If you select element (""), only the first node of this class is returned. The LINQ syntax briefly explains that the from clause specifies the selected range, where specifies the selected conditions, and selete specifies the selected target. If do not understand can put the code practice debugging look at, have different ideas brother contact me: zhaotiantang520@live.cn.

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.