反序列化,从XML中取数据

XML文件部分:
 
<? xml version="1.0" encoding="utf-8"  ?>
< shiporder  orderid ="889923"
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation
="shiporder.xsd" >
  
< orderperson > George Bush </ orderperson >
  
< shipto >
    
< name > John Adams </ name >
    
< address > Oxford Street </ address >
    
< city > London </ city >
    
< country > UK </ country >
  
</ shipto >
  
< item >
    
< title > Empire Burlesque </ title >
    
< note > Special Edition </ note >
    
< quantity > 1 </ quantity >
    
< price > 10.90 </ price >
  
</ item >
  
< item >
    
< title > Hide your heart </ title >
    
< quantity > 1 </ quantity >
    
< price > 9.90 </ price >
  
</ item >
</ shiporder >

 

 XSD文件部分:

 

<? xml version="1.0" encoding="iso-8859-1"  ?>
< xs:schema  xmlns:xs ="http://www.w3.org/2001/XMLSchema" >

  
< xs:element  name ="shiporder" >
    
< xs:complexType >
      
< xs:sequence >
        
< xs:element  name ="orderperson"  type ="xs:string" />
        
< xs:element  name ="shipto" >
          
< xs:complexType >
            
< xs:sequence >
              
< xs:element  name ="name"  type ="xs:string" />
              
< xs:element  name ="address"  type ="xs:string" />
              
< xs:element  name ="city"  type ="xs:string" />
              
< xs:element  name ="country"  type ="xs:string" />
            
</ xs:sequence >
          
</ xs:complexType >
        
</ xs:element >
        
< xs:element  name ="item"  maxOccurs ="unbounded" >
          
< xs:complexType >
            
< xs:sequence >
              
< xs:element  name ="title"  type ="xs:string" />
              
< xs:element  name ="note"  type ="xs:string"  minOccurs ="0" />
              
< xs:element  name ="quantity"  type ="xs:positiveInteger" />
              
< xs:element  name ="price"  type ="xs:decimal" />
            
</ xs:sequence >
          
</ xs:complexType >
        
</ xs:element >
      
</ xs:sequence >
      
< xs:attribute  name ="orderid"  type ="xs:string"  use ="required" />
    
</ xs:complexType >
  
</ xs:element >

</ xs:schema >

 

.CS文件部分:

 

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Web;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Xml;
using  System.Xml.Schema;
using  System.IO;
using  System.Xml.Serialization;

namespace  Demo
{
    
public   partial   class  WebForm1 : System.Web.UI.Page
    {
        
protected   void  Page_Load( object  sender, EventArgs e)
        {
            XmlReaderSettings settings 
=   new  XmlReaderSettings();
            settings.IgnoreWhitespace 
=   true ;
            settings.IgnoreComments 
=   true ;
            NameTable nt 
=   new  NameTable();
            
string  link  =  nt.Add( " shipto " );
            settings.NameTable 
=  nt;

            
// 验证
             string  strSchemaFileSrc  =   base .ResolveUrl(Path.Combine(Request.PhysicalApplicationPath,  " shiporder.xsd " ));
            settings.Schemas.Add(
null , XmlReader.Create(strSchemaFileSrc));
            settings.ValidationType 
=  ValidationType.Schema;
            settings.ValidationFlags 
=  XmlSchemaValidationFlags.ReportValidationWarnings;
            settings.ValidationEventHandler 
+=   new  ValidationEventHandler(settings_ValidationEventHandler);

            
// 序列化工厂类
            XmlSerializerFactory factory  =   new  XmlSerializerFactory();
            
string  strDataFileSrc  =   base .ResolveUrl(Path.Combine(Request.PhysicalApplicationPath,  " shiporder.xml " ));
            
using  (XmlReader reader  =  XmlReader.Create(strDataFileSrc, settings))
            {
                
while  (reader.Read())
                {
                    
if  (reader.NodeType  ==  XmlNodeType.Element  &&  String.Compare(link, reader.LocalName,  true ==   0 )
                    {
                        XmlSerializer xs 
=  factory.CreateSerializer( typeof (shipto));
                        
// 获取到对象,对象中已包含xml中的数据
                        shipto l  =  (shipto)xs.Deserialize(reader.ReadSubtree());
                        
                    }
                }
            }
        }

        
private   void  settings_ValidationEventHandler( object  sender, System.Xml.Schema.ValidationEventArgs e)
        {
            
throw   new  Exception( " 数据文件:  "   +   ""   +   "  格式不正确! [ "   +  e.Message  +   " ] " );
        }
    }

    
public   class  shipto
    {
        
private   string  _name;
        
private   string  _address;
        
private   string  _city;
        
private   string  _country;

        
public   string  name
        {
            
get  {  return  _name; }
            
set  { _name  =  value; }
        }

        
public   string  address
        {
            
get  {  return  _address; }
            
set  { _address  =  value; }
        }
        
public   string  city
        {
            
get  {  return  _city; }
            
set  { _city  =  value; }
        }
        
public   string  country
        {
            
get  {  return  _country; }
            
set  { _country  =  value; }
        }
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值