将XML反序列化为对象列表时跳过元素

时间:2016-02-04 15:34:27

标签: c# xml deserialization

我有以下XML:

<OUTPUT_XML>
        <VehicleMakes>
            <VehicleMake VehMakeId="1" MakeName="VOLKSWAGEN" VehTypeId="1"/>
            <VehicleMake VehMakeId="2" MakeName="OPEL" VehTypeId="1"/>
            <VehicleMake VehMakeId="3" MakeName="FORD" VehTypeId="1"/>
            <VehicleMake VehMakeId="4" MakeName="VAZ" VehTypeId="1"/>
            <VehicleMake VehMakeId="5" MakeName="FIAT" VehTypeId="1"/>
        </VehicleMakes>
</OUTPUT_XML>

我需要将其序列化为一个对象,但我需要跳过<OUTPUT_XML>并仅反序列化VehicleMakes。作为清单。我在一个表示OUTPUT_XML的类中使用了XmlArray和XmlArrayItem,但是如果我尝试通过编写跳过该类(OUTPUT_XML):

 public class GetMakesResponse
        {
            [XmlElement("exec_id")]
            public string ExecId { get; set; }

            [XmlElement("ERROR_CODE")]
            public int ErrorCode { get; set; }

            [XmlElement("VehicleMake")]
            public List<VehicleMake> VehicleMakes { get; set; }
        }

xml序列化程序失败,集合为空。

感谢。

ADD:

目前我使用以下类:

 [Serializable]
        [XmlRoot("root")]
        public class MakesResponse
        {
            [XmlElement("exec_id")]
            public string ExecId { get; set; }

            [XmlElement("ERROR_CODE")]
            public int ErrorCode { get; set; }

            [XmlElement("OUTPUT_XML")]
            public Output Output { get; set; }
        }

[Serializable]
    public class VehicleMake
    {
        [XmlAttribute("VehMakeId")]
        public int VehMakeId { get; set; }

        [XmlAttribute("MakeName")]
        public string MakeName { get; set; }

        [XmlAttribute("VehTypeId")]
        public short VehTypeId { get; set; }
    }

[Serializable]
    public class Output
    {
        [XmlArray(ElementName = "VehicleMakes")]
        [XmlArrayItem(ElementName = "VehicleMake")]
        public List<VehicleMake> VehicleMakes { get; set; }
    }

反序列化的代码:

public class XmlSerializationUtility<T>
    {
        private readonly XmlSerializer _serializer;

        public XmlSerializationUtility()
        {
            _serializer = new XmlSerializer(typeof(T));
        } 

        public T Deserialize(string xml)
        {
            using (var mStream = new MemoryStream(Encoding.UTF8.GetBytes(xml)))
            {
                return (T)_serializer.Deserialize(mStream);
            }
        }

        public string Serialize<TInput>(TInput obj)
        {
            using (var sw = new StringWriter())
            using (var writer = XmlWriter.Create(sw))
            {
                _serializer.Serialize(writer, obj);
                return sw.ToString();
            }
        }
    }

这样可以正常工作,但我想跳过输出ID。

1 个答案:

答案 0 :(得分:0)

您可以使用XmlTextReader循环遍历各个VehicleMake元素,并按照以下方式单独反序列化它们:

XmlSerializer serializer = new XmlSerializer(typeof(VehicleMake));

List<VehicleMake> result = new List<VehicleMake>();

XmlTextReader reader = new XmlTextReader(new MemoryStream(Encoding.UTF8.GetBytes(xml)));

if(reader.ReadToDescendant("VehicleMake"))
{
    do
    {
        result.Add((VehicleMake)serializer.Deserialize(reader));
    } while (reader.ReadToNextSibling("VehicleMake"));
}

请注意,您的XML看起来像这样(ArrayOfVehicleMake而不是VehicleMakes):

<OUTPUT_XML>
    <ArrayOfVehicleMake>
        <VehicleMake VehMakeId="1" MakeName="VOLKSWAGEN" VehTypeId="1"/>
        <VehicleMake VehMakeId="2" MakeName="OPEL" VehTypeId="1"/>
        <VehicleMake VehMakeId="3" MakeName="FORD" VehTypeId="1"/>
        <VehicleMake VehMakeId="4" MakeName="VAZ" VehTypeId="1"/>
        <VehicleMake VehMakeId="5" MakeName="FIAT" VehTypeId="1"/>
    </ArrayOfVehicleMake>
</OUTPUT_XML>

您可以对整个列表进行反序列化,而不会像这样循环遍历各个元素:

XmlSerializer serializer = new XmlSerializer(typeof(List<VehicleMake>));

XmlTextReader reader = new XmlTextReader(new MemoryStream(Encoding.UTF8.GetBytes(xml)));

reader.ReadToDescendant("ArrayOfVehicleMake");

var result = (List<VehicleMake>) serializer.Deserialize(reader);