About XmlSerialize

Topics: Developer Forum
Aug 29, 2008 at 12:16 PM

your Sample SerializeClass code in Unit.Test can Serialize and Deserilize well.I tested.

But, the code below can XmlSerialize But Can not DeSerialize

-----------------------------------------------------------------------------------------------
 [Serializable]
    public abstract class Sitex : LinqObjectModel<Sitex>
    {
        public abstract string Url { get; set; }
    }

------------------------------------------------------------------
  Sitex s = Sitex.New();
                s.Url = "ddd";
                string c2 = XmlSerializer<Sitex>.Xml.Serialize((Sitex)s);
------------------------------------------------------------------
c2  is right:
 <?xml version="1.0"?>
<Sitex>
  <Id>0</Id>
  <Url>ddd</Url>
</Sitex>
----------------------------------------------------------------
                var f = XmlSerializer<Sitex>.Xml.Deserialize(c2);

--------------------------------------------------------------------
when Deserialize,it will throw exception
    There is an error in XML document (2, 2).

-----------------------------------------------------------------
And...

--------------------------------------------------------------------------
Sitex s = Sitex.New();
                s.Url = "ddd";
                System.Xml.XmlWriter x1 = System.Xml.XmlWriter.Create("c:\\xxx.txt");
                x1.WriteStartElement("xxx1");
                s.WriteXml(x1);
                x1.WriteEndElement();
                x1.Flush();
                x1.Close();
                Sitex s2 = Sitex.New();
                System.Xml.XmlReader x2 = System.Xml.XmlReader.Create("c:\\xxx.txt");
                s2.ReadXml(x2);
哈哈ReadXml居然是空的,晕死。。。。

有没有想过用json呢?Json.net...

Aug 29, 2008 at 1:54 PM
我尝试写这个ReadXML,但是到了赋值阶段我就不会写了,能否不穷举FieldType 呢?

public void ReadXml(System.Xml.XmlReader reader)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(reader);
            ObjectInfo oi = ObjectInfo.GetInstance(this.GetType());
            foreach (MemberHandler mh in oi.SimpleFields)
            {
                string name = mh.MemberInfo.Name;
                string value = doc.DocumentElement.SelectSingleNode(name).FirstChild.Value;
                if (mh.FieldType == typeof(long))
                {
                    mh.SetValue(this, long.Parse(value));
                }else if (mh.FieldType == typeof(string))
                {
                    mh.SetValue(this, (string)value);
                }
              
            }
        }
Aug 29, 2008 at 2:05 PM
Edited Aug 29, 2008 at 2:40 PM
这样吧,这样写应该可以搞定enum,还有什么需要注意的你觉得?
 public void ReadXml(System.Xml.XmlReader reader)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(reader);
            ObjectInfo oi = ObjectInfo.GetInstance(this.GetType());
            foreach (MemberHandler mh in oi.SimpleFields)
            {
                string name = mh.MemberInfo.Name;
                string value = doc.DocumentElement.SelectSingleNode(name).FirstChild.Value;
                if (mh.FieldType == typeof(long))
                {
                    mh.SetValue(this, long.Parse(value));
                }
                else if (mh.FieldType == typeof(int))
                {
                    mh.SetValue(this, int.Parse(value));
                }
                else if (mh.FieldType == typeof(string))
                {
                    mh.SetValue(this, (string)value);
                }
                else if (mh.FieldType.IsEnum)
                {
                    mh.SetValue(this, Enum.Parse(mh.FieldType, value, true));
                }
              
            }
        }
Coordinator
Aug 29, 2008 at 2:53 PM
json 和 xml 一样需要自己写代码。
通用代码都会因为是纯虚类,而无法创建吧。
至于你写的代码,我觉得需要处理的ValueType应该不止int和long,还有guid、byte、short、double、single、sbyte等等。
另外最好能有byte[]的,不过这个可能需要序列化的代码也更改才行吧。
其实以前加入IXmlSerializable主要是想让实体类可以直接作为WebService的参数,后来发现还要实现GetSchema,这个比ReadXml更麻烦,就放下了……
Coordinator
Aug 29, 2008 at 2:54 PM
Lephone.UnitTest.Data.FullTypesTest 里有所有DbEntry支持(或应该支持)的类型。
Coordinator
Aug 29, 2008 at 3:27 PM
如果不想穷举类型,可以使用Convert.ChangeType吧。
Aug 29, 2008 at 4:14 PM
谢谢,Convert.ChangeType很有用,收下了,不过不是万能的,enum不能用。谢谢了。
ReadXml很有用,我再想想。
微软搞的WebService我不看好,你没做是正确的。o(∩_∩)o...