文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>XML 自定义串行化_ASP.NET技巧

XML 自定义串行化_ASP.NET技巧

时间:2010-10-14  来源:蔚蓝的大海

      使用xml串行化就可以将类的实例转换为XML,反之亦然。开发人员经常需要进行XML穿行化。
      程序清单中展示了一个串行化的实例。第一步是创建 Person  类的实例,并用进行初始化。然后利用XmlSerializer将Person  类的这个实例串行化。 using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/**//// <summary>
/// Person 的摘要说明
/// </summary>
public class Person
{
    public Person()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    public string Name
    {
        set
        {
            _name = value;
        }
        get
        {
            return _name;
        }
    }   PRivate string _name;

    public int Age
    {
        set
        {
            _age = value;
        }
        get
        {
            return _age;
        }
    }   private int _age;
}

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml.Serialization" %>

<script runat="server">
   
    protected void Page_Load(object sender, EventArgs e)
    {
        Person person = new Person();
        person.Name = "☆聊ゾ聊☆";
        person.Age = 23;
        //Response.Clear();
        //Response.ContentType = "text/xml";
        XmlSerializer serializer = new XmlSerializer(person.GetType());
        serializer.Serialize(Response.OutputStream, person);
    }
</script>
运行后得到的结果: <?xml version="1.0"?>
<Person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>☆聊ゾ聊☆</Name>
  <Age>23</Age>
</Person>
自定义串行化。假设需要把某个类的一个共有属性转换为 XML 属性,那么上面的做法就无法满足要求了。为了达到这一目的,我们可以实现自定义的XML串行化。程序清单如下: using System;
using System.Xml.Serialization;

[XmlRoot("员工")]
public class Employee
{
    public Employee()
    {
    }

    [XmlAttribute("姓名")]
    public string Name
    {
        set
        {
            _name = value;
        }
        get
        {
            return _name;
        }
    }   private string _name;

    [XmlAttribute("年龄")]
    public int Age
    {
        set
        {
            _age = value;
        }
        get
        {
            return _age;
        }
    }   private int _age;

    [XmlElement("备注")]
    public string Remarks
    {
        set
        {
            _remarks = value;
        }
        get
        {
            return _remarks;
        }
    }   private string _remarks;
}

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Xml.Serialization" %>

<script runat="server">
   
    protected void Page_Load(object sender, EventArgs e)
    {
        Employee employee = new Employee();
        employee.Name = "☆聊ゾ聊☆";
        employee.Age = 23;
        employee.Remarks = "☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆";
        //Response.Clear();
        //Response.ContentType = "text/xml";
        XmlSerializer serializer = new XmlSerializer(employee.GetType());
        serializer.Serialize(Response.OutputStream, employee);
    }
</script>

运行结果: <?xml version="1.0"?>
<员工 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 姓名="☆聊ゾ聊☆" 年龄="23">
  <备注>☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆ ☆聊ゾ聊☆</备注>
</员工>

http://www.cnblogs.com/hblynn/archive/2007/02/09/646403.html


Replica LV M803R Louis Vuitton LV Handbags Montaigne Bag - RedReplica LV M804BK Louis Vuitton LV Handbags Madeleine GM - BlackReplica LV M804PL Louis Vuitton LV Handbags Madeleine GM - GrenadeReplica LV M805BK Louis Vuitton LV Handbags Madeleine PM - BlackReplica LV M805PL Louis Vuitton LV Handbags Madeleine PM - GrenadeReplica LV RedM5 Louis Vuitton LV Handbags Saint-Jacques Poignées LonguesReplica Louis Vuitton Epi Leather Alma Bag Black GM M52892 HandbagsReplica Louis Vuitton Epi Leather Alma Bag Dark Red M5280M HandbagsReplica Louis Vuitton Epi Leather Bagatelle Black PM M40232 M40232-black HandbagsReplica Louis Vuitton Epi Leather Bagatelle PM Purple M40232 M40232-purple HandbagsReplica Louis Vuitton Epi Leather Bowling Montaigne Black M59332 HandbagsReplica Louis Vuitton Epi Leather Bowling Montaigne PM Purple M59322 HandbagsReplica Louis Vuitton Epi Leather Bucket PM Red M58992 HandbagsReplica Louis Vuitton Epi Leather Cannes Cosmetic Bag Orange M48032 HandbagsReplica Louis Vuitton Epi Leather Croisette MM Black M5250 HandbagsReplica Louis Vuitton Epi Leather Houston Bag Purple M91005 HandbagsReplica Louis Vuitton Epi Leather Speedy 30 Blue M41526 M41526-blue HandbagsReplica Louis Vuitton Epi Leather Speedy 30 Red M59022 M59022-red HandbagsReplica Louis Vuitton Epi Leather Top Handle Bag Black 59042 HandbagsReplica Louis Vuitton Epi Leather Top Handle Bag Blue 52046 Handbags
相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载