XmlSerializer is not trustable

XmlSerializer is stupid in many ways..., it did cause me a lot of trouble.

1.       Normally we use it to serialize a class to xml stream by calling Serialize() method, it this  method  has several overloaded versions. When XmlSerializerNamespaces is specified as input parameter it is supposed to generate a prefix right before the xml tab, e.g. Serialize(Stream stream, Object o, XmlSerializerNamespaces namespaces ), but it does not always generate the expected result like <prefix:tagname..>. It could be caused by different .Net framework versions installed, but the inconsistent behavior cause a lot of tricky problems.

2.       System.Xml.Serialization.XmlElementAttribute is used to determine what the xml tag for current field is after serialization, for example.

Public  class DemoClass

{

Private string  m _Name;

System.Xml.Serialization.XmlElementAttribute(“FullName”)

Public string Name

{

                Get {return m_Name;}

                Set {m_Name =value;}

}

}

 

After serialization, the corresponding  xml tag for Name should be FullName, but XmlSerializer does not always output FullName, sometimes it will output Name directly. You also can find a tons of XmlSerializer related bug descriptions on internet, google it.

posted @ 2009-01-08 00:09  zhanqiangz(闲云野鹤)  阅读(321)  评论(0编辑  收藏  举报