LINQ and some difference between XDocument and XElement

ContractedBlock.gif ExpandedBlockStart.gif 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
ContractedBlock.gif ExpandedBlockStart.gif 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 Objects
Introduction to LINQ, Part 2: LINQ to XML
Introduction to LINQ, Part 3: LINQ to SQL

转载于:https://www.cnblogs.com/feishunji/archive/2009/08/04/1538574.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值