在序列化期间控制XmlType元素名称

时间:2011-02-24 21:09:43

标签: .net serialization abstract-class

我有一个类,它是一个抽象类的列表

[XmlInclude(typeof(PostedPayment))]
[XmlInclude(typeof(PostedInvoice))]
[XmlType("VoucherProgress")]
public class PostedJournals : List<APostedJournal>
{
    public PostedJournals(IEnumerable<APostedJournal> postedJournals) : base(postedJournals) { }
    public PostedJournals() { }
}

但是当我序列化时,我最终会

<VoucherProgress>
  <APostedJournal p2:type="PostedPayment" xmlns:p2="http://www.w3.org/2001/XMLSchema-instance">
      ...
  </APostedJournal>
</VoucherProgress>

当我想要命名类型时,不要使用抽象名称

<VoucherProgress>
  <PostedPayment>
      ...
  </PostedPayment>
</VoucherProgress>

有没有办法让这项工作具有一些属性?

1 个答案:

答案 0 :(得分:0)

在这里,我找到了你这个:

< ?xml version="1.0"?>
< MyList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  < MyTypeBase />
  < MySubType>something</ MySubType>
  < MyTypeBase />
  < MyTypeBase />
  < MyOtherSubType>something</ MyOtherSubType>
  < MyTypeBase />
  < MyTypeBase />
  < MySubType>something</ MySubType>
  < MyTypeBase />
  < MyTypeBase />
  < MyOtherSubType>something</ MyOtherSubType>
  < MyTypeBase />
  < MyTypeBase />
  < MySubType>something</ MySubType>
  < MyTypeBase />
</ MyList >

(我不知道如何在这个爆炸的网站上粘贴XML。

课程计划     {         static void Main(string [] args)         {             MyList tmp = new MyList();

        tmp.Add(new MySubType());
        tmp.Add(new MyOtherSubType());
        tmp.Add(new MySubType());
        tmp.Add(new MyOtherSubType());
        tmp.Add(new MySubType());

        XmlSerializer xs = new XmlSerializer(typeof(MyList));

        MemoryStream ms = new MemoryStream();
        xs.Serialize(ms, tmp);

        ms.Seek(0, SeekOrigin.Begin);
        TextReader tr = new StreamReader(ms);
        string xt = tr.ReadToEnd();

    }
}

[XmlRoot(ElementName="MyList")]
public class MyList : List<MyTypeBase> { }

[XmlInclude(typeof(MySubType))]
public class MyTypeBase : IXmlSerializable
{
    public int ID { get; set; }

    protected virtual Type elType { get { return typeof(MyTypeBase); } }

    public System.Xml.Schema.XmlSchema GetSchema()
    {
        return null;
    }

    public void ReadXml(System.Xml.XmlReader reader)
    {

    }

    public void WriteXml(System.Xml.XmlWriter writer)
    {
        writer.WriteEndElement();
        writer.WriteStartElement(elType.Name);

        writer.WriteValue("something");

        writer.WriteEndElement();
        writer.WriteStartElement("MyTypeBase");
    }
}

public class MySubType : MyTypeBase, IXmlSerializable
{
    public string Name { get; set; }
    protected override Type elType { get { return typeof(MySubType); } }
}

public class MyOtherSubType : MyTypeBase, IXmlSerializable
{
    public string Name { get; set; }
    protected override Type elType { get { return typeof(MyOtherSubType); } }
}

代码会生成您想要的列表,但是您可以自己计算出其他的MtTypeBase元素。所有这一切都改变了基类的序列化方式。每个子类都将elType更改为它自己的类类型,因此基类可以使用它来完成它。我已经花了很多时间在这上面,所以你可以在这里形成它。有很多方法可以改善这种状况并使其更好更清洁。