本文介绍了反序列化XML数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用单个元素进行反序列化。但是当我有xml元素数组时,我的代码无效。



下面是我的代码



我尝试过:



I got deserializing working with single element. But when I have array of xml elements, my code is not working.

Below is my code

What I have tried:

<data>
    <cars>
        <body>
            <color>blue<color>
            <type>sedan</type>
        </body>
        <details>
            <year>2016</year>
            <make>Infiniti</make>
        </details>
    </cars>
    <cars>
        <body>
            <color>white<color>
            <type>SUV</type>
        </body>
        <details>
            <year>2016</year>
            <make>Lexus</make>
        </details>
    </cars>
</data>



Dto


Dto

[XmlRoot("cars")]
public class CarDetails
{
	[XmlElement("body")]
	public Body BodyList { get; set; }

	[XmlElement("details")]
	public DetailsList details { get; set; }
}

public class Body
{
	public string Color { get; set; }
	public string Type { get; set; }
}

public class DetailsList
{
	public int Year { get; set; }
	public string Make { get; set; }
}



以下是反序列化的代码:


Below is the code for deserializing:

CarDetails[] details;
XmlSerializer serializer = new XmlSerializer(typeof(CarDetails[]));
using (TextReader reader = new StringReader(output))
{
    details = (CarDetails[])serializer.Deserialize(reader);
}

推荐答案

string xml = File.ReadAllText(@"test.xml");
List<CarDetails> res = new List<CarDetails>();
var serializer = new XmlSerializer(res.GetType());

using (TextReader reader = new StringReader(xml))
{
	res = (List<CarDetails>)serializer.Deserialize(reader);
}


<color>blue<color>



应该是:


should be:

<color>blue</color>



与:
相同


same with:

<color>white<color>



应该是:


should be:

<color>white</color>



接下来,您需要为XML生成类。这里有两个选择:



选项1。(首选)



这是一个从XML生成类的有用工具: []



生成此:


Next, You need to generate classes for your XML. Here are two options:

Option 1. (Preferred)

Here is a useful tool to generate classes from XML: [^]

Generates this:

/*
    Licensed under the Apache License, Version 2.0

    http://www.apache.org/licenses/LICENSE-2.0
    */
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
    [XmlRoot(ElementName="body")]
    public class Body {
        [XmlElement(ElementName="color")]
        public string Color { get; set; }
        [XmlElement(ElementName="type")]
        public string Type { get; set; }
    }

    [XmlRoot(ElementName="details")]
    public class Details {
        [XmlElement(ElementName="year")]
        public string Year { get; set; }
        [XmlElement(ElementName="make")]
        public string Make { get; set; }
    }

    [XmlRoot(ElementName="cars")]
    public class Cars {
        [XmlElement(ElementName="body")]
        public Body Body { get; set; }
        [XmlElement(ElementName="details")]
        public Details Details { get; set; }
    }

    [XmlRoot(ElementName="data")]
    public class Data {
        [XmlElement(ElementName="cars")]
        public List<Cars> Cars { get; set; }
    }

}



选项2。



此外,Visual Studio(在VS2017上测试)也可以从XML生成类:

1.将XML数据复制到剪贴板

2.在VS中,编辑>选择性粘贴> 将Xml粘贴为类



生成此:


Option 2.

Also, Visual Studio (tested on VS2017) can also generate classes from XML:
1. Copy XML data to the clipboard
2. In VS, Edit > Paste Special > "Paste Xml as classes"

Generates this:

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class data
{

    private dataCars[] carsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("cars")]
    public dataCars[] cars
    {
        get
        {
            return this.carsField;
        }
        set
        {
            this.carsField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class dataCars
{

    private dataCarsBody bodyField;

    private dataCarsDetails detailsField;

    /// <remarks/>
    public dataCarsBody body
    {
        get
        {
            return this.bodyField;
        }
        set
        {
            this.bodyField = value;
        }
    }

    /// <remarks/>
    public dataCarsDetails details
    {
        get
        {
            return this.detailsField;
        }
        set
        {
            this.detailsField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class dataCarsBody
{

    private string colorField;

    private string typeField;

    /// <remarks/>
    public string color
    {
        get
        {
            return this.colorField;
        }
        set
        {
            this.colorField = value;
        }
    }

    /// <remarks/>
    public string type
    {
        get
        {
            return this.typeField;
        }
        set
        {
            this.typeField = value;
        }
    }
}

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class dataCarsDetails
{

    private ushort yearField;

    private string makeField;

    /// <remarks/>
    public ushort year
    {
        get
        {
            return this.yearField;
        }
        set
        {
            this.yearField = value;
        }
    }

    /// <remarks/>
    public string make
    {
        get
        {
            return this.makeField;
        }
        set
        {
            this.makeField = value;
        }
    }
}



因此,对于您的问题,选项1。用于收集汽车:


So for your question, Option 1. uses for the collection of cars:

[XmlElement(ElementName="cars")]
public List<Cars> Cars { get; set; }



选项2。使用:


and Option 2. uses:

private dataCars[] carsField;

/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("cars")]
public dataCars[] cars
{
    get
    {
        return this.carsField;
    }
    set
    {
        this.carsField = value;
    }
}



可以简化为:


Which could be simplified as:

private dataCars[] carsField;

/// <remarks/>
[System.Xml.Serialization.XmlElement("cars")]
public dataCars[] cars
{
    get => carsField;
    set => carsField = value;
}


这篇关于反序列化XML数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 01:51