简体   繁体   English

XmlSerializer使用默认XmlRoot序列化

[英]XmlSerializer Serialize with a default XmlRoot

Is there anyway to add a XML Root Element or "Wrapper" in the XmlSerializer when I serialize an object? 当我序列化一个对象时,是否在XmlSerializer中添加XML根元素或“包装器”?

The XML I am looking for would be something like this: 我正在寻找的XML将是这样的:

<Groups>
    <Group method="ModifySubGroups" ID="1234" PIN="5678">
        <SubGroup action="Delete" number="95">
            <Name>Test</Name>
        </SubGroup>
    </Group>
</Groups>

I have two classes, Group and SubGroup. 我有两个类,组和子组。 Group contains a generic list of SubGroups. 组包含子组的常规列表。 It works great, but I don't have the XML Root "Groups". 它工作得很好,但是我没有XML根目录“ Groups”。 Using the two classes Group and SubGroup produces this: 使用Group和SubGroup这两个类将产生以下结果:

<Group method="ModifySubGroups" ID="1234" PIN="5678">
   <SubGroup action="Delete" number="95">
       <Name>Test</Name>
   </SubGroup>
</Group>

The only way I could get it to work was to create another class "Groups" that contained Group. 我可以使它起作用的唯一方法是创建另一个包含Group的类“ Groups”。 So now I have three classes, Groups, Group, and SubGroup. 所以现在我有三个类,Groups,Group和SubGroup。 Groups contains Group and Group contains SubGroup. 组包含组,组包含子组。

Any other ideas? 还有其他想法吗?

You don't normally use XML serialization to make XML pretty. 通常,您不会使用XML序列化来使XML变得漂亮。 If you need a root container element, then you need to have a root container object, and serialize that instead of Group object. 如果需要根容器元素,则需要有一个根容器对象,并将其序列化而不是Group对象。

You can however serialize an array of Group object 但是,您可以序列化Group对象的数组

void Main()
{
    var g = new Group();
    g.SubGroups.Add(new SubGroup {Name = "aaa"});

    var ser = new XmlSerializer(typeof(Group[]), new XmlRootAttribute("Groups"));
    using (var w = new StringWriter())
    {
        ser.Serialize(w, new Group[] {g});
        w.ToString().Dump();
    }
}

public class Group
{
    [XmlElement("SubGroup")]
    public List<SubGroup> SubGroups = new List<SubGroup>();
}

public class SubGroup
{
    public string Name;
}

Naturally this means that deserialize code needs to either magically know that there is always one and only one Group element or assume that there could be 0 or more. 自然地,这意味着反序列化代码需要神奇地知道总是只有一个Group元素,或者假设可能有0或更多。 Honestly I don't see much point in doing this unless you actually want to serialize collection of groups. 老实说,除非您确实想序列化组的集合,否则这样做没有什么意义。 It would just add confusion. 这只会增加混乱。

EDIT: if you really want to comply to vendors schema you are starting from wrong point. 编辑:如果您真的想遵守供应商架构,则从错误的角度出发。 You do not need to implement classes like this at all, all you do instead is taking an vendors XSD and use xsd utility provided with Visual Studio to generate .net classes from your schema, you can also choose which way you want serialize objects - using XmlSerializer or DataContractSerializer ( which gives you better flexibility i would say ) 您根本不需要实现这样的类,而要做的只是获取供应商XSD并使用Visual Studio随附的xsd实用程序从架构中生成.net类,还可以选择要序列化对象的方式-使用XmlSerializerDataContractSerializer (我会说这为您提供了更好的灵活性)

NOTE : you can use some tools to generate xsd from your xml if you do not have one and do not know how to write it on yourself 注意:如果您没有XML并且不知道如何在自己身上编写它,则可以使用一些工具从xml生成xsd。

You can use XmlRootAttribute in order to specify custom XML Root 您可以使用XmlRootAttribute来指定自定义XML根

Also when you serialize collection you can specify wrapper - see Array Serializing 另外,在序列化集合时,您可以指定包装器-请参见数组序列化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM