当序列化XML时更改元素顺序

32

我需要将一个对象序列化为XML并还原回来。XML格式是固定的,我不能更改它。 我无法在bookingList之后生成这种结构。

如何将这些<booking>元素“分组”以作为一个列表出现,并保留在这个<booking>元素列表之前的<error><counter>元素。

查看我的示例:

我所需的结构...

<nicexml>
<key_id>1234567</key_id>
<surname>Jil</surname>
<name>Sander</name>
<station_id>1</station_id>
<ownBookings>
    <bookingList>
        <error></error>
        <counter>20</counter>
        <booking>
             <bookingID>1234567890</bookingID>
        </booking>
        <booking>
             <bookingID>2345678901</bookingID>
        </booking>
    </bookingList>
</ownBookings>
</nicexml>

使用以下C#代码获得的结构...

<nicexml>
<key_id>1234567</key_id>
<surname>Jil</surname>
<name>Sander</name>
<station_id>1</station_id>
<ownBookings>
    <bookingList>
           <booking>
        <booking>
             <bookingID>1234567890</bookingID>
        </booking>
        <booking>
             <bookingID>2345678901</bookingID>
        </booking>
             <booking>
        <error></error>
        <counter>20</counter>
    </bookingList>
</ownBookings>
</nicexml>

C# 代码:

using System;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace xml_objects_serials
{
    public class bookings
    {
        public class nicexml
        {
            public string key_id
            { get; set; }

            public string surname
            { get; set; }

            public string name
            { get; set; }

            public int station_id
            { get; set; }

            public ownBookings ownBookings
            { get; set; }

        }

        public class ownBookings
        {
            public bookingList bookingList
            { get; set; }

        }
        public class bookingList {
            public string error 
            { get; set; }
            public int counter
            { get; set; }
            public List<booking> booking= new List<booking>();
        }

        public class booking
        {
            public int bookingID
            { get; set; }
        }
    }
2个回答

47
尝试使用XmlElementAttribute来装饰bookingList类的属性,以便控制该类对象如何序列化为XML。下面是一个例子:
public class bookingList
{
    [XmlElement(Order = 1)]
    public string error { get; set; }
    [XmlElement(Order = 2)]
    public int counter { get; set; }
    [XmlElement(ElementName = "booking", Order = 3)]
    public List<booking> bookings = new List<booking>();
}

public class booking
{
    public int id { get; set; }
}

在我的测试中,我得到了这个输出:

<?xml version="1.0" ?> 
<bookingList>
    <error>sample</error>
    <counter>0</counter>
    <booking>
        <id>1</id> 
    </booking>
    <booking>
        <id>2</id> 
    </booking>
    <booking>
        <id>3</id> 
    </booking> 
</bookingList>

相关资源:


我不知道这个“elementorder”。非常感谢! - Alexey Honorio

-4

我曾经遇到过这个问题,但是我解决了它...嗯,这很有趣,也许这是 .net 中的一个 bug。

问题出在这里: public List<booking> booking= new List<booking>();

你应该使用: public List<booking> booking { get; set; }

然后你就会得到定义的顺序....但为什么呢?谁知道呢... :)


如果它的工作方式是这样的,那么这似乎是一个奇怪的怪癖,我不会依赖这个功能。 - Raphael Smit

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接