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

发布于 2024-10-19 03:19:04 字数 772 浏览 3 评论 0原文

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

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

有没有办法让它与某些属性一起工作?

I have a class that is a list of an abstract class

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

But when I serialize, I end up with

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

When I want it to name the types, not use the abstract name

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

Is there a way to make this work with some attributes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

止于盛夏 2024-10-26 03:19:04

在这里,我已经告诉你了:(

< ?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 粘贴到这个该死的网站上。class

Program
{
静态无效主(字符串[]参数)
{
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 更改为它自己的类类型,以便基类可以使用它来完成它的任务。我已经在这上面花了很多时间,所以你可以在这里了解它。有很多方法可以改进这一点并使其更好、更清洁。

Here, I got you this far:

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

(I have NO idea how to get XML pasted on this blasted site.

class Program
{
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); } }
}

The code makes the list you want but with additional MtTypeBase elements which you can figure out on your own. All it does it changes the way the base class is serialized. Each subclass changes the elType to it's own class type so the base class can use it to do it's thing. I already spent a ton of time on this so you can take it form here. Plenty of ways to improve this and make it better and cleaner.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文