XmlArray 序列化 - 如何让序列化程序忽略列表中项目的类名? [英] XmlArray Serialization - How can I make the serializer ignore the class name of items in a list?

查看:36
本文介绍了XmlArray 序列化 - 如何让序列化程序忽略列表中项目的类名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类,其中包含一个 MyObject 列表.

I have a class that, among other properties, has a list of MyObject.

public class MyClass
{
    ...

    [XmlArray("OBJECT")]
    public List<MyObject> Objects { get; set; }

    ...
}

每个 MyObject 有两个属性,PropA 和 PropB.

Each MyObject has two properties, PropA and PropB.

public class MyObject
{
    public string PropA {get;set;}
    public string PropB {get;set;}
}

XML 序列化程序返回此 XML:

The XML Serializer returns this XML:

<OBJECT>
    <MyObject>
        <PropA>A1</PropA>
        <PropB>B1</PropB>
    </MyObject>
    <MyObject>
        <PropA>A2</PropA>
        <PropB>B2</PropB>
    </MyObject>
    <MyObject>
        <PropA>A3</PropA>
        <PropB>B3</PropB>
    </MyObject>
</OBJECT>

我给定的要求是让它看起来像这样(我发誓,考虑到真实的对象名称和属性值,它看起来没有那么错误,但无论如何,这是我的任务):

My given requirements are for it to look like this (I swear, it looks much less wrong given the real object names and property values, but regardless, this is my task):

<OBJECT>
    <PropA>A1</PropA>
    <PropB>B1</PropB>
    <PropA>A2</PropA>
    <PropB>B2</PropB>
    <PropA>A3</PropA>
    <PropB>B3</PropB>
</OBJECT>

实现这一目标的最佳方法是什么?我尝试使用各种 Xml 属性(例如 XmlArrayItem、XmlElement),但我似乎无法摆脱类名包装器.

What is the best way to make this happen? I tried messing around with various Xml attributes (e.g. XmlArrayItem, XmlElement), but I can't seem to get rid of the class name wrapper.

我已经考虑过自定义 xml 序列化,但我讨厌扔掉发生在其他属性上的所有(工作)默认 Xml 序列化,每个属性上只有一个 XmlElement 属性.

I have considered custom xml serialization, but I hate to throw away all the (working) default Xml serialization that happens on the other properties with only an XmlElement attribute on each of them.

有什么想法吗?

谢谢.

如果您指的是 xDaevax 善意发布的链接,这不是重复的.(XML 序列化 - 禁用渲染数组的根元素).这个问题/答案不是问/解决如何摆脱项目类名称(列表中每个项目周围的标签),它展示了如何摆脱包含整个项目列表的整体列表标签.我想要的恰恰相反.如果有另一个指向类似的已回答问题的链接,我很想知道.谢谢.

This is not a duplicate if you are referring to the link that xDaevax kindly posted. (XML Serialization - Disable rendering root element of array). This question/answer isn't asking/solving how to get rid of the item class names (the tags around each item in the list), it shows how to get rid of the overall list tag that encompasses the entire list of items. I want precisely the reverse. If there is another link to a similar, answered question, I would love to know. Thank you.

推荐答案

所以,我做了一些非常棘手的事情,但它有效,我想这才是最重要的.我将 PropA 更改为 int,因为它始终是一个数字,并使用以下标记来设置我希望 XML 序列化程序使用的特殊属性.我使用了 Selman22 的一些建议来简化它.(谢谢!)

So, I did something pretty hacky, but it works and I suppose that's all that matters. I changed PropA to be an int, since it was always a number, and used the following tags to set up the special property I wanted the XML serializer to use. I used some of Selman22's suggestion to make that easier. (Thank you!)

不幸的是,如果您的属性都是相同的类型,则此解决方案将不起作用,但它满足我目前的需求,我们预计不会有任何更改的可能性(这种情况很少见).

This solution will not work if your properties are all the same type, unfortunately, but it meets my needs at present and we do not foresee any potential for change (rare as that is).

[XmlIgnore]
public List<MyObject> Objects{ get; set; }

[XmlArray(ElementName = "OBJECT")]
[XmlArrayItem(Type = typeof(int), ElementName = "PROPA")]
[XmlArrayItem(Type = typeof(string), ElementName = "PROPB")]
public List<object> XmlObjects
{
    get
    {
        var xmlObjects = new List<object>();

        if (Objects != null)
        {
            xmlObjects.AddRange(Objects.SelectMany(x => new List<object>
            {
                x.PropA,
                x.PropB
            }));
        }

        return xmlObjects;
    }
    set { }
}

这篇关于XmlArray 序列化 - 如何让序列化程序忽略列表中项目的类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆