LINQ and some difference between XDocument and XElement

Source: Internet
Author: User
Tags xml example
App. config
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>

<MyConfig>
<Products>
<ProductItem Property1 = "xyz" Property2 = "abc"/>
<ProductItem Property1 = "sddf" Property2 = "ljd"/>
<ProductItem Property1 = "oiu" Property2 = "sldj"/>
</Products>
<Store>
<StoreItem Property1 = "xyz" Property2 = "abc"/>
<StoreItem Property1 = "sddf" Property2 = "ljd"/>
</Store>
<File Name = "HelloWorld" Type = "doc"/>
<ErrorCode Message = "This is incorrect" Length = "20"/>
</MyConfig>

<ConfigSections>
</ConfigSections>

<ConnectionStrings>
<Add name = "WindowsFormsApplication1.Properties. Settings. Setting"
ConnectionString = "Data Source = SHA-baiei-vista; Initial Catalog = test; Integrated Security = True"
ProviderName = "System. Data. SqlClient"/>
</ConnectionStrings>

</Configuration>

Winform code: XElement config = XElement. Load (AppDomain. CurrentDomain. SetupInformation. ConfigurationFile );
Foreach (XElement xe in config. Elements ())
TextBox1.Text + = (xe. Name. ToString () + Environment. NewLine );

XDocument xd = XDocument. Load (AppDomain. CurrentDomain. SetupInformation. ConfigurationFile );
Foreach (XElement xe in xd. Elements ())
TextBox1.Text + = (xe. Name. ToString () + Environment. NewLine );

Output:
MyConfig
ConfigSections
ConnectionStrings
Configurationsome other XLINQ samples
XElement doc = XElement. Load (@ "D: \ sample. xml ");

XDocument contactsDoc = new XDocument (
New XDeclaration ("1.0", "UTF-8", "yes "),
New XComment ("LINQ to XML Contacts XML Example "),
New XProcessingInstruction ("MyApp", "123-44-4444 "),
New XElement ("contacts ",
New XElement ("contact ",
New XElement ("name", "Patrick Hines "),
New XElement ("phone", "206-555-0144 "),
New XElement ("address ",
New XElement ("street1", "123 Main St "),
New XElement ("city", "Mercer Island "),
New XElement ("state", "WA "),
New XElement ("postal", "68042 ")
))));

XElement contactsDoc2 = XElement. Parse (@ "<contacts>
<Contact>
<Name> Patrick Hines </name>
<Phone> 206-555-0144 </phone>
<Address>
<Street1> 123 Main St </street1>
<City> Mercer Island </city>
<State> WA </state>
<Postal> 68042 </postal>
</Address>
</Contact>
</Contacts> ");

IEnumerable <XElement> contacts = contactsDoc. Elements ("contact ");
ContactsDoc. Save (@ "C: \ sample. xml ");
ContactsDoc2.Save (@ "C: \ sample2.xml ");
Doc. Save (@ "C: \ sampleResult. xml ");

There, u can c the differences between XElement and XDocument
In the past, I wrote some XML operation tutorials in non-LINQ time: http://hi.baidu.com/feishunji/blog/category/Xml

Marius BancilaHas also contributed some good introduction articles of LINQ:
Introduction to LINQ, Part 1: LINQ to Objects
Introduction to LINQ, Part 2: LINQ to XML
Introduction to LINQ, Part 3: LINQ to SQL

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.