something about XmlSerializer

原创 2007年09月29日 15:20:00

 

This week ,i research  something about XmlSerializer.  now ,i want to talk something about this  subject.

in order to transfers data,usual we should  do xml serialize and deserialize,so we should

microsoft visual studio2005  provide a class XmlSerializer to do xml serialize,frist of all ,we should know xml structure,include xmlroot ,xmlelement, xmlattribute and so on.

Example:in this expression

<DocumentElement>

       <temp userid= "4">

              <name>1111</name>

      </userid>

<DocumentElement>

<DocumentElement> is the xmlroot ,and <name> is xmlelement ,</userid> is xmlattribute,so you should know this concept.

so when serialize the xml ,so we should get the object  type which will be serialize.         Example :   XmlSerializer ser = new XmlSerializer(o.GetType());through this sentence we get a xmlserializer object ,

the second,we should  create a xmltextwriter to editor xml data.if we want to create,we must create a memory stream  use to store data stream which has been  by xmltextwriter.

Example:

            System.IO.MemoryStream mem = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8);

and the last step we can serialize the data stream,and close xmltextwriter.

Example:

          ser.Serialize(writer, o, ns);

ok,now we have already complish  xml serialize.

all code:

        public static string myXmlSerializer(object o)
        {
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("", "");
            XmlSerializer ser = new XmlSerializer(o.GetType());
            System.IO.MemoryStream mem = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(mem, Encoding.UTF8);
            ser.Serialize(writer, o, ns);
            writer.Close();
            return Encoding.UTF8.GetString(mem.ToArray());
        }

about Deserialize,like serialize ,we also should get the string and xml type.the two thing has been transfers as parament.

Code Example:

      public static object myXmlDeserialize(string s, Type t)
        {
            XmlSerializer mySerializer = new XmlSerializer(t);
            StreamReader mem2 = new StreamReader(new MemoryStream(System.Text.Encoding.UTF8.GetBytes(s)), System.Text.Encoding.UTF8);
            return mySerializer.Deserialize(mem2);
        }

i will write something about  how to construct.

【HDU 5984 Pocky】+ 数学

Pocky Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Su...
  • WYK1823376647
  • WYK1823376647
  • 2016年12月04日 21:37
  • 679

解决:ruby We're sorry, but something went wrong.We've been notified about this issue and we'll take a look at it shortly.

本人是ruby on rails 的初学者,前几天碰到了如上所示的问题,google了老半天,终于在一篇老外的博客上发现了问题,原来查看log目录下的development.log发现了问题所在“!/...
  • zhong871004
  • zhong871004
  • 2009年07月24日 15:57
  • 8729

在使用apt-get安装报错:To continue type in the phrase 'Yes, do as I say!' ?] 解决方法

在使用apt-get安装软件包时可能会出现类似如下错误: WARNING: The following essential packages will be removed. This should ...
  • Sky_qing
  • Sky_qing
  • 2013年08月01日 21:03
  • 3352

杭电5984之Pocky

Pocky Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi...
  • ESESZB
  • ESESZB
  • 2017年03月31日 22:06
  • 449

HDU5984 Pocky

Pocky Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Su...
  • xky140610205
  • xky140610205
  • 2016年11月28日 22:24
  • 604

Something about ...

一直都觉得《Friends》是部典型的肥皂剧,就像以前国内的《我爱我家》一样,纯粹用来消磨时间而已。 最开始知道《Friends》大概还是在两三年前吧,网上的一帮《Friends》的fans把这片子吹...
  • CrazyKyro
  • CrazyKyro
  • 2004年10月20日 14:48
  • 399

Something about the HTK tool

The first speech recognizer and trainer most speech researcher works on if they started after 99. (I...
  • xiaoding133
  • xiaoding133
  • 2011年12月20日 16:29
  • 637

something about function on python

today i read the official document of python3.1 the Defining function chapter introduce some konwled...
  • DecellHoo
  • DecellHoo
  • 2009年08月24日 17:21
  • 262

something about twitter

一点想法 找到有影响力的人之后,可不可以找到这群人所具备的特点(从发微博的特征,个人描述来看)。了解为什么会被影响。...
  • forevsmile
  • forevsmile
  • 2013年08月23日 11:45
  • 235

something about PCIe

Went over my excerpts on PCIe protocol which I made before. Although I really dont have time for tha...
  • farhilltramper
  • farhilltramper
  • 2011年08月06日 11:23
  • 199
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:something about XmlSerializer
举报原因:
原因补充:

(最多只允许输入30个字)