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


       <temp userid= "4">




<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.


            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.


          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);
            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.

something about tinyos

  • 2014年05月16日 22:49
  • 795KB
  • 下载

something about Educational Codeforces Round 11

这场CF的E、F两题最近才补,拿来说说。比较有意思的是,官方题解给的都不是最优复杂度。。E. Different Subsets For All Tuples  这道题肯定是dp,我的做法是这样的。假...

Something about Compile error

A strange compile error! error :expected unqualified-id before ‘using’ It occurs When a class is d...

something about me

每当谈起自己的故事呢,经常会自以为曲折动人,坎坷万千,但并不想提及自己的故事,仅想谈谈自己的现在。 我是正在努力成为一名合格程序猿的人,对,现在看来,仅仅只祈求着合格,其实在报志愿的时候,我最钟爱的...

Something about Torrents.

List some important definitions about torrents. 关于种子文件 P2P 技术的一些概念解释。...

Something about software suspend (swsusp)

from: Something about software suspend (swsu...
  • rex_nie
  • rex_nie
  • 2013年06月20日 14:14
  • 800

something about Compile and Link on Embedded Linux

1 , Compile 流程 2,The `##' preprocessing operator performs token pasting #define mypr...
  • jslywj
  • jslywj
  • 2013年11月06日 16:51
  • 645

Something about CPP

Long time no see, old guys!(means C++)Undefined Meaning In c++ STL, there is hash function, so don’...

Something about Java

1. java.lang.String.endsWith() 方法返回的测试该字符串是否以指定后缀sffix结束 2. 返回的文件和目录在此抽象路径名指定的目录...
  • aqzwss
  • aqzwss
  • 2014年11月17日 21:23
  • 374

Something about C

What the hell? How long since I touch with C? What a pity, I have to work with it now.Pointer To ...
您举报文章:something about XmlSerializer