Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何解析具有多个属性和选项列表的XML_C#_Xml_Xml Parsing_Xmlserializer_Xml Attribute - Fatal编程技术网

C# 如何解析具有多个属性和选项列表的XML

C# 如何解析具有多个属性和选项列表的XML,c#,xml,xml-parsing,xmlserializer,xml-attribute,C#,Xml,Xml Parsing,Xmlserializer,Xml Attribute,我有一个类似以下内容的XML文件: <root> <data label="product data" min="0" max="10"> <option> <id>1</id> <name>Name1</name> </option> <option> <

我有一个类似以下内容的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>
选修课:

选项

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; }
}
一般选项

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; }
}
但当我尝试反序列化对象时,它会启动以下异常:

命名空间“”中的XML元素“data”已存在于当前作用域中。使用XML属性为元素指定另一个XML名称或命名空间

我想问题是我试图“两次”检索同一元素。但我需要把这两样东西都收回来。我不能使用
[Attribute]
这件事,因为有几个属性需要检索,我需要用几个格式相同的XML元素来检索,我想重用它


那么,我如何才能同时检索这两个文件呢?

您需要稍微重新构造它:

[XmlRoot(“根”)]
公共类数据
{
[XmlElement(“数据”)]
公共选项数据选项{get;set;}
}
公共类选项数据
{
[XmlAttribute(“标签”)]
公共字符串标签{get;set;}
[xmldattribute(“min”)]
公共字符串Min{get;set;}
[xmldattribute(“max”)]
公共字符串Max{get;set;}
[XmlElement(“期权”)]
公共列表项{get;}=new List();
}
公共类通用选项
{
[XmlElement(“id”)]
公共字符串Id{get;set;}
[XmlElement(“名称”)]
公共字符串名称{get;set;}
}

您需要稍微重新构造它:

[XmlRoot(“根”)]
公共类数据
{
[XmlElement(“数据”)]
公共选项数据选项{get;set;}
}
公共类选项数据
{
[XmlAttribute(“标签”)]
公共字符串标签{get;set;}
[xmldattribute(“min”)]
公共字符串Min{get;set;}
[xmldattribute(“max”)]
公共字符串Max{get;set;}
[XmlElement(“期权”)]
公共列表项{get;}=new List();
}
公共类通用选项
{
[XmlElement(“id”)]
公共字符串Id{get;set;}
[XmlElement(“名称”)]
公共字符串名称{get;set;}
}
我建议使用XML或任何其他工具在几秒钟内将XML转换为C#Models。。。(消除所有手动错误)

正如@canton7所提到的,另一个简单的方法是使用
visualstudio:Edit->Paste Special->Paste XML As class

[XmlRoot(ElementName=“option”)]
公共类选项{
[xmlement(ElementName=“id”)]
公共字符串Id{get;set;}
[xmlement(ElementName=“name”)]
公共字符串名称{get;set;}
}
[XmlRoot(ElementName=“data”)]
公共类数据{
[xmlement(ElementName=“option”)]
公共列表选项{get;set;}
[XmlAttribute(AttributeName=“label”)]
公共字符串标签{get;set;}
[XmlAttribute(AttributeName=“min”)]
公共字符串Min{get;set;}
[XmlAttribute(AttributeName=“max”)]
公共字符串Max{get;set;}
}
[XmlRoot(ElementName=“root”)]
公共类根{
[xmlement(ElementName=“data”)]
公共数据数据{get;set;}
}
我建议使用XML或任何其他工具在几秒钟内将XML转换为C#Models。。。(消除所有手动错误)

正如@canton7所提到的,另一个简单的方法是使用
visualstudio:Edit->Paste Special->Paste XML As class

[XmlRoot(ElementName=“option”)]
公共类选项{
[xmlement(ElementName=“id”)]
公共字符串Id{get;set;}
[xmlement(ElementName=“name”)]
公共字符串名称{get;set;}
}
[XmlRoot(ElementName=“data”)]
公共类数据{
[xmlement(ElementName=“option”)]
公共列表选项{get;set;}
[XmlAttribute(AttributeName=“label”)]
公共字符串标签{get;set;}
[XmlAttribute(AttributeName=“min”)]
公共字符串Min{get;set;}
[XmlAttribute(AttributeName=“max”)]
公共字符串Max{get;set;}
}
[XmlRoot(ElementName=“root”)]
公共类根{
[xmlement(ElementName=“data”)]
公共数据数据{get;set;}
}

(对于最初感到困惑的其他人,
数据
类为
元素建模,
选项数据
类为
元素建模)(对于最初感到困惑的其他人来说,
数据
类为
元素建模,
选项数据
类为
元素建模)(请注意,Visual Studio中内置了一个模块:编辑->粘贴特殊->将XML粘贴为类。不过,它会生成非常详细的C)(请注意,VisualStudio中内置了一个:编辑->粘贴特殊->将XML粘贴为类。不过,它会生成非常详细的C#)