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-fifei-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
configuration
some 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 Bancila has also contributed some good introduction articles of LINQ:
Introduction to LINQ, Part 1: LINQ to ObjectsIntroduction to LINQ, Part 2: LINQ to XMLIntroduction to LINQ, Part 3: LINQ to SQL