.net与XML——LINQ To Xml

入门的,先举一例

 

ContractedBlock.gif ExpandedBlockStart.gif Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace mylqConApp
{
    
class lqxml
    {
        
static void Main()
        {

            XDocument doc 
= new XDocument(
                
new XDeclaration("1.0""utf-8""yes"),
                
//元素节点
                new XElement("books",
                    
new XElement("Book",
                        
new XAttribute("ID""1"),
                        
new XElement("NO""1254-004"),
                        
new XElement("Name""Book 004"),
                        
new XElement("Price""30.00"),
                        
new XElement("Remark""this is a book about conputer")
                    ),
                    
new XElement("Book",
                        
new XAttribute("ID""2"),
                        
new XElement("NO""1254-005"),
                        
new XElement("Name""Book 005"),
                        
new XElement("Price""35.00"),
                        
new XElement("Remark""this is a book about conputer")
                    ),
                    
new XElement("Book",
                        
new XAttribute("ID""3"),
                        
new XElement("NO""1254-015"),
                        
new XElement("Name""Book 015"),
                        
new XElement("Price""135.00"),
                        
new XElement("Remark""this is a book about conputer")
                    )
               )
            );

            
//Serialize this System.Xml.Linq.XDocument to a file.
            doc.Save("c:\\tt.xml");

            
//Creates a new System.Xml.Linq.XDocument from a file.
            XDocument xdoc = XDocument.Load("c:\\tt.xml");
            Console.WriteLine(xdoc.ToString());

            
//查询根元素名称
            IEnumerable<XElement> root = from e in xdoc.Elements("books")
                                         select e;

            
foreach (var s in xdoc.Elements())
            {
                Console.WriteLine(s.Name);
            }
            
foreach (XElement xe in root)
            {
                Console.WriteLine(
"查询根元素名称 " + xe.Name);
            }

            
//Loads an System.Xml.Linq.XElement from a file
            XElement xElem = XElement.Load("c:\\tt.xml");
            
//查询指定名称的元素

            IEnumerable
<XElement> ele = from e in xElem.Elements("Book")
                                        
where (string)e.Element("Name"== "Book 005"
                                        select e;
            
//或者
            
//IEnumerable<XElement> ele = from e in xdoc.Elements("books").Elements("Book")
            
//                            where (string)e.Element("Name") == "Book 005"
            
//                            select e;

            
foreach (var u in ele)
            {
                Console.WriteLine(
"ID " + u.Attribute("ID").Value + " name " + u.Name);
            }

            
//Add
            XElement book = new XElement("Book",
                        
new XAttribute("ID""4"),
                        
new XElement("NO""1254-045"),
                        
new XElement("Name""Book 045"),
                        
new XElement("Price""135.00"),
                        
new XElement("Remark""this is a book about conputer"));
            xElem.Add(book);
            
//生成添加新元素之后的xml文件
            xElem.Save("c:\\t.xml");



        }
    }
}

真的是入门级的,自己留着看看……

转载于:https://www.cnblogs.com/hsrzyn/archive/2009/11/17/1604862.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值