Linq to XML说法——(一)创建,添加,查询

简介: Xml操作 场景:产品采购单。 描述:产品采购单用于描述产品的采购,它可以从各地进行采购,且每地可以采购多种商品。地址信息包括:城市,电话,联系人,日期,商品;商品包括0到多项,商品包括:产品名称,编号,描述,单价,采购总量。

Xml操作

场景:产品采购单。

描述:产品采购单用于描述产品的采购,它可以从各地进行采购,且每地可以采购多种商品。地址信息包括:城市,电话,联系人,日期,商品;商品包括0到多项,商品包括:产品名称,编号,描述,单价,采购总量。

<? xml version="1.0" encoding="utf-8"  ?>
< PurchaseOrder >
  
< address >
    
< city ></ city >
    
< call ></ call >
    
< contact ></ contact >
    
< opdate ></ opdate >
    
< products >
      
< product >
        
< name ></ name >
        
< num ></ num >
        
< price ></ price >
        
< total ></ total >
        
< description ></ description >
      
</ product >
    
</ products >
  
</ address >
</ PurchaseOrder >

 

(一)创建单子

创建声明

XDocument doc  =   new  XDocument();
doc.Declaration 
=   new  XDeclaration( " 1.0 " , " utf-8 " , " no " );

 

说明:Xdocument所以名字空间:System.Xml.Linq

(二)添加根元素

doc.Add( new  XElement( " purchaseOrder " ));

 

(三)添加地址address元素

doc.Element( " purchaseOrder " ).Add( new  XElement( " address " ));
            doc.Element(
" purchaseOrder " ).Element( " address " ).
                Add(
                
new  XElement( " city " ),
                
new  XElement( " call " ),
                
new  XElement( " contact " ),
                
new  XElement( " opdate " ),
                
new  XElement( " products " )
                );

 

(四)Linq to xml添加产品

// 定义产品列表
IList < product >  _list  =   new  List < product > () { 
new  product{ name = " 产品1 " , num = " B001 " , price = 12 ,total = 20 ,description = " 产品1描述 " },
new  product{ name = " 产品2 " , num = " B002 " , price = 16 ,total = 22 ,description = " 产品2描述 " }
}; 

// 添加产品
doc.Element( " purchaseOrder " ).Element( " address " ).Element( " products " ).Add
   (
    from q 
in  _list 
    select 
new  XElement( " product " ,
    
new  XElement( " name " ,q.name),
    
new  XElement( " num " ,q.num),
    
new  XElement( " price " , q.price),
    
new  XElement( " total " , q.total),
    
new  XElement( " description " , q.description)
    ));

 

(五)设置元素值

// 为city,call,contact,opdate设置值
var _addressList  =  from q  in  doc.Elements( " purchaseOrder " ).Elements( " address " ) select q;

foreach  (XElement dd  in  _addressList)
{
    
foreach  (XElement e1  in  (from p  in  dd.Elements() select p))
    {
        
switch  (e1.Name.ToString())
        { 
        
case   " city " :
               e1.SetValue(
" 石家庄 " );
              
break ;
        
case   " call " :
               e1.SetValue(
" 86868666 " );
               
break ;
        
case   " contact " :
              e1.SetValue(
" 暂无联系方式 " );
               
break ;
        
case   " opdate " :
               e1.SetValue(
" 2009-12-21 " );
               
break ;
        }
    }
}

 

(六)保存文件

doc.Save( @" E:\test8\LinqTest\LToXml\source\PurchaseOrder.xml " );

 

(七)在最后一个产品之后加一个新产品

doc.Element( " purchaseOrder " ).Element( " address " ).Element( " products " )
                .Add(
new  XElement( " product " ,
                
new  XElement( " name " " 产品3 " ),
                
new  XElement( " num " " C003 " ),
                
new  XElement( " price " 18 ),
                
new  XElement( " total " 108 ),
                
new  XElement( " description " " 产品3 " )
                ));

 

(八)在第一个产品这前添加一个新产品

doc.Element( " purchaseOrder " ).Element( " address " ).Element( " products " ).AddFirst
                (
                
new  XElement( " product " ,
                
new  XElement( " name " " 产品4 " ),
                    
new  XElement( " num " " C004 " ),
                    
new  XElement( " price " 66 ),
                    
new  XElement( " total " 27 ),
                    
new  XElement( " description " " 产品4 " ))
                );

 

(九)产品列表

// 得到产品列表
var productList  =  from q  in  doc.Root
                 .Element(
" address " )
                 .Element(
" products " )
                 .Elements(
" product " )
                 select q;

 

            //这个列表如下:

            /************************************************

            Name  num       price   total       description           

            产品4 C004        66       27          产品4

            产品1 B001        12       20          产品1描述

            产品2 B002        16       22          产品2描述

            产品3 C003        18       108         产品3

            **************************************************/

(十)可以根据这个表进行linq查询 

// 查询产品总量
var iTotal  =  productList.Sum(p => ( int )p.Element( " total " ));

// 查询总价
var iTotalPrice  =  productList.Sum(q  =>  ( int )q.Element( " price " *  ( int )q.Element( " total " ));

// 查询描述是"产品3"的产品
var product3  =  from q  in  productList  where  ( string )q.Element( " description " ) ==   " 产品3 "  
               select 
new  product{ name = ( string )q.Element( " name " ),
                                   num
= ( string )q.Element( " num " ),
                                   price
= ( int )q.Element( " price " ),
                                   total
= ( int )q.Element( " total " ),
                                   description
= ( string )q.Element( " description " ),
                           };

 

博客园大道至简

http://www.cnblogs.com/jams742003/

转载请注明:博客园

目录
相关文章
|
XML 存储 开发框架
Linq To XML总结
Linq To XML总结
85 0
Linq To XML总结
C#动态创建Xml-LinQ方式
C#创建Xml-LinQ方式 本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) Chinar —— ...
1130 0
|
XML 数据格式
XML——对XML文档的创建与增删改查
原文:XML——对XML文档的创建与增删改查 一、创建的第一种方式  //1、创建一个XML文档 XmlDocument doc = new XmlDocument(); //2、创建第一行描述信息 XmlDeclaration dec = doc.
946 0
|
XML JavaScript .NET
|
XML JavaScript .NET
|
XML 数据格式 .NET
linq to xml
无废话,直接上: 1 try 2 { 3 XDocument xdoc = new XDocument( 4 new XProcessingInstruction("UserConfigHelp", "xmlns:xsi='http://www.
787 0
|
XML .NET 数据格式
|
SQL XML JavaScript
Querying Microsoft SQL Server 2012 读书笔记:查询和管理XML数据 1 -使用FOR XML返回XML结果集
原文:Querying Microsoft SQL Server 2012 读书笔记:查询和管理XML数据 1 -使用FOR XML返回XML结果集 XML 介绍 ...
1016 0

热门文章

最新文章

相关课程

更多