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