如何使用多个属性和选项列表解析 XML

How to parse XML with several Attributes and an options list

我有一个 XML 文件,看起来与此类似:

<root>
    <data label="product data" min="0" max="10">
        <option>
            <id>1</id>
            <name>Name1</name>
        </option>
        <option>
            <id>2</id>
            <name>Name2</name>
        </option>
        <option>
            <id>3</id>
            <name>Name3</name>
        </option>
    </data>
</root>

我需要检索 data 属性和选项列表。

我试过这个:

[XmlRoot(ElementName = "root")]
public class Data
{
    // Retreive data attributes
    [XmlElement(ElementName = "data")]
    public Options Attributes { get; set; }

    // Retrieve option list
    [XmlArray("data")]
    [XmlArrayItem("option", Type = typeof(GeneralOptions))]
    public GeneralOptions[] Options { get; set; }
}

可选类:

选项

public class Options
{
    [XmlAttribute("label")]
    public string Label{ get; set; }

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

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

常规选项

public class GeneralOptions
{
    [XmlElement(ElementName = "id")]
    public string Id { get; set; }

    [XmlElement(ElementName = "name")]
    public string Name{ get; set; }
}

但是当我尝试反序列化该对象时,它会引发以下异常:

The XML element 'data' from namespace '' is already present in the current scope. Use XML attributes to specify another XML name or namespace for the element.

我想问题是我正在尝试检索相同的元素 "twice"。但我需要收回这两件事。我不能使用 [Attribute] 东西,因为有几个属性要检索,我需要用几个具有相同格式的 XML 元素来做到这一点,我想重用它。

那么,我怎样才能取回它们?

您需要稍微重组它:

[XmlRoot("root")]
public class Data
{
    [XmlElement("data")]
    public OptionsData Options { get; set; }
}

public class OptionsData
{
    [XmlAttribute("label")]
    public string Label { get; set; }

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

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

    [XmlElement("option")]
    public List<GeneralOptions> Items { get; } = new List<GeneralOptions>();
}

public class GeneralOptions
{
    [XmlElement("id")]
    public string Id { get; set; }

    [XmlElement("name")]
    public string Name { get; set; }
}

我建议使用 xmltocsharp 或任何其他工具在几秒钟内将 XML 转换为 C# 模型...(消除所有手动错误)

As mentioned by @canton7, another easy method is using Visual Studio: Edit -> Paste Special -> Paste XML As Classes

[XmlRoot(ElementName="option")]
public class Option {
    [XmlElement(ElementName="id")]
    public string Id { get; set; }
    [XmlElement(ElementName="name")]
    public string Name { get; set; }
}

[XmlRoot(ElementName="data")]
public class Data {
    [XmlElement(ElementName="option")]
    public List<Option> Option { get; set; }
    [XmlAttribute(AttributeName="label")]
    public string Label { get; set; }
    [XmlAttribute(AttributeName="min")]
    public string Min { get; set; }
    [XmlAttribute(AttributeName="max")]
    public string Max { get; set; }
}

[XmlRoot(ElementName="root")]
public class Root {
    [XmlElement(ElementName="data")]
    public Data Data { get; set; }
}