<?xml version="1.0" encoding="utf-8"?>
<Nodeprops xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Text>哈哈</Text>
  <FillColor />
  <Action>jump</Action>
  <loop_play>false</loop_play>
  <mystep_code>second</mystep_code>
  <father_node>大胆</father_node>
  <FileName>萨达</FileName>
  <ErroyFileName>阿斯顿</ErroyFileName>
  <ID>2</ID>
</Nodeprops><?xml version="1.0" encoding="utf-8"?>
<Nodeprops xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Text>中心</Text>
  <FillColor />
  <Action>jump</Action>
  <loop_play>true</loop_play>
  <mystep_code>second</mystep_code>
  <father_node>1</father_node>
  <FileName>d://temp</FileName>
  <ErroyFileName>散打</ErroyFileName>
  <ID>0</ID>
</Nodeprops><?xml version="1.0" encoding="utf-8"?>
<Snodeprops xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ID>1</ID>
  <Text>分中心</Text>
  <FillColor />
  <FileName>dfd</FileName>
  <ErroyFileName>fd</ErroyFileName>
</Snodeprops>
这是我序列化生成的一个xml文档,现在的问题是我想反序列化可是我这里面有俩个属性类Snodeprops和Nodeprops,XmlSerializer在反序列化的时候要求必须给一个属性,可是现在我这xml文档里面有俩个,所以我想重写XmlSerializer方法,让它可以再打开xml文件后一个属性一个属性反序列化,但是做了好久没头绪,请高手指点,最好有事例源码,小弟感激涕零,在线等啊!

解决方案 »

  1.   

    如果是可以有继承关系的,使用XmlIncludeAttribute
    例如:
    [XmlInclude(typeof(B))]
    [XmlInclude(typeof(C))]
    public class A{}
    public class B : A{}
    public class C : A{}//...
    XmlSerializer s = new XmlSerializer(typeof(A));
    //...
      

  2.   


    我现在生成的这个XML文件里面有好多不同的属性类
    在反序列化还原的时候  object mp = (Employee)bformatter.Deserialize(stream);
     Employee属性是只能是一个的
      

  3.   

    晕死,拜托请运行一下再说
    [XmlInclude(typeof(B))]
    [XmlInclude(typeof(C))]
    public class A
    {
      public string PA{get;set;}
    }
    public class B : A
    {
      public string PB{get;set;}
    }
    public class C : A
    {
      public string PC{get;set;}
    }
    // 请运行下面的代码(如果不是vs2008,请自行改写)
    XmlSerializer s = new XmlSerializer(typeof(A));
    s.Serialize(Console.Out, new B
    {
      PA="PA",
      PB="PB",
    }
    Console.WriteLine("**************");
    s.Serialize(Console.Out, new C
    {
      PA="PA",
      PC="PC",
    }
      

  4.   

    我试了下不行
    你这个还是没解决我的问题
    你也是每个类序列化
    最后各自生成一个xml文件
    我现在就想生成在一个xml文件里面
    然后在各自反序列化
      

  5.   

    我觉得有些奇怪,楼主把所有类序列化后放入一个文档中,为什么不用一个集合来做,对集合做序列化.像楼主列出来的那个 Xml 文档,那就不是一个正确的 xml 文档啊, 楼主可以用 XmlDocument 或 XmlReader 载入试一下, 马上会抛出异常.
      

  6.   

    浅序列化,试试深序列化为Soap
      

  7.   

    刚做了下测试, 不知道是否合楼主的要求:
    [XmlInclude(typeof(Nodeprops))]
    public class Snodeprops
    {
    public Snodeprops() { }
    public Snodeprops(int id) { Id = id; }
    public int Id { get; set; }
    }public class Nodeprops : Snodeprops
    {
    public Nodeprops() { }
    public Nodeprops(int id, string text) : base(id) { Text = text; }
    public string Text { get; set; }
    }序列化及反序列化:
    public static void XmlSerial()
    {
    Snodeprops[] nodes = {
    new Snodeprops(1),
    new Snodeprops(2),
    new Nodeprops(3, "three"),
    new Nodeprops(4, "four")
    }; XmlSerializer serial = new XmlSerializer(typeof(Snodeprops[]));
    System.Text.StringBuilder sbXml = new System.Text.StringBuilder();
    XmlWriter xw = XmlWriter.Create(sbXml);
    serial.Serialize(xw, nodes); // 序列化
    xw.Close();
    System.IO.StringReader sr = new System.IO.StringReader(sbXml.ToString());
    Snodeprops[] deNodes = serial.Deserialize(sr) as Snodeprops[]; // 反序列化
    sr.Close();
    Console.WriteLine(((Nodeprops)deNodes[2]).Text);
    }