Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/336.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# 使用XmlArray反序列化xml文件?_C#_.net_Xml_Serialization_Xml Serialization - Fatal编程技术网

C# 使用XmlArray反序列化xml文件?

C# 使用XmlArray反序列化xml文件?,c#,.net,xml,serialization,xml-serialization,C#,.net,Xml,Serialization,Xml Serialization,我正在尝试反序列化这个xml结构 <?xml version="1.0"?> <DietPlan> <Health> <Fruit>Test</Fruit> <Fruit>Test</Fruit> <Veggie>Test</Veggie> <Veggie>Test</Veggie> &

我正在尝试反序列化这个xml结构

<?xml version="1.0"?>
<DietPlan>
    <Health>
        <Fruit>Test</Fruit>
        <Fruit>Test</Fruit>
        <Veggie>Test</Veggie>
        <Veggie>Test</Veggie>
    </Health>
</DietPlan>
但这引发了一个异常“XML元素已存在于当前作用域中。请使用XML属性为元素指定另一个XML名称或命名空间。”
感谢adv.

您需要一个通用类型来反序列化XML,并且使用它,您可以使用
[xmlement]
名称空间根据元素的名称定义要实例化的类型,如下所示

public class StackOverflow_15907357
{
    const string XML = @"<?xml version=""1.0""?>
                        <DietPlan>
                            <Health>
                                <Fruit>Test</Fruit>
                                <Fruit>Test</Fruit>
                                <Veggie>Test</Veggie>
                                <Veggie>Test</Veggie>
                            </Health>
                        </DietPlan>";

    [XmlRoot(ElementName = "DietPlan")]
    public class TestSerialization
    {
        [XmlArray("Health")]
        [XmlArrayItem("Fruit", Type = typeof(Fruit))]
        [XmlArrayItem("Veggie", Type = typeof(Veggie))]
        public Food[] Foods { get; set; }
    }

    [XmlInclude(typeof(Fruit))]
    [XmlInclude(typeof(Veggie))]
    public class Food
    {
        [XmlText]
        public string Text { get; set; }
    }

    public class Fruit : Food { }
    public class Veggie : Food { }

    public static void Test()
    {
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
        XmlSerializer xs = new XmlSerializer(typeof(TestSerialization));
        TestSerialization obj = (TestSerialization)xs.Deserialize(ms);
        foreach (var food in obj.Foods)
        {
            Console.WriteLine("{0}: {1}", food.GetType().Name, food.Text);
        }
    }
}
公共类StackOverflow_15907357
{
常量字符串XML=@“
试验
试验
试验
试验
";
[XmlRoot(ElementName=“DietPlan”)]
公共类TestSerialization
{
[XmlArray(“健康”)]
[XmlArrayItem(“水果”,Type=typeof(水果))]
[XmlArrayItem(“素食者”,Type=typeof(素食者))]
公共食品[]食品{get;set;}
}
[xmlclude(类型(水果))]
[xmlclude(类型(素食))]
公共食品
{
[XmlText]
公共字符串文本{get;set;}
}
公共类水果:食品{}
公共类素食者:食物{}
公共静态无效测试()
{
MemoryStream ms=新的MemoryStream(Encoding.UTF8.GetBytes(XML));
XmlSerializer xs=新的XmlSerializer(typeof(TestSerialization));
TestSerialization obj=(TestSerialization)xs.反序列化(ms);
foreach(对象食品中的变量食品)
{
WriteLine(“{0}:{1}”,food.GetType().Name,food.Text);
}
}
}
public class StackOverflow_15907357
{
    const string XML = @"<?xml version=""1.0""?>
                        <DietPlan>
                            <Health>
                                <Fruit>Test</Fruit>
                                <Fruit>Test</Fruit>
                                <Veggie>Test</Veggie>
                                <Veggie>Test</Veggie>
                            </Health>
                        </DietPlan>";

    [XmlRoot(ElementName = "DietPlan")]
    public class TestSerialization
    {
        [XmlArray("Health")]
        [XmlArrayItem("Fruit", Type = typeof(Fruit))]
        [XmlArrayItem("Veggie", Type = typeof(Veggie))]
        public Food[] Foods { get; set; }
    }

    [XmlInclude(typeof(Fruit))]
    [XmlInclude(typeof(Veggie))]
    public class Food
    {
        [XmlText]
        public string Text { get; set; }
    }

    public class Fruit : Food { }
    public class Veggie : Food { }

    public static void Test()
    {
        MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(XML));
        XmlSerializer xs = new XmlSerializer(typeof(TestSerialization));
        TestSerialization obj = (TestSerialization)xs.Deserialize(ms);
        foreach (var food in obj.Foods)
        {
            Console.WriteLine("{0}: {1}", food.GetType().Name, food.Text);
        }
    }
}