C# 读取XML文件并将其解析为类列表

C# 读取XML文件并将其解析为类列表,c#,xml,winforms,visual-studio-2013,C#,Xml,Winforms,Visual Studio 2013,这是我用来将列表中的数据写入XML文件的代码: using (FileStream f = new FileStream(@"animals.xml", FileMode.Create, FileAccess.Write)) { var dcsw = new DataContractSerializer(typeof(List<Animal>)); XmlDictionaryWriter writ

这是我用来将列表中的数据写入XML文件的代码:

using (FileStream f = new FileStream(@"animals.xml", FileMode.Create, FileAccess.Write))
            {
                var dcsw = new DataContractSerializer(typeof(List<Animal>));
                XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(f);

                try
                {
                    dcsw.WriteObject(f, animalList);
                    writer.Close();
                    f.Close();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
<ArrayOfAnimal xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/AnimalShelter">
<Animal i:type="Cat">
<ChipRegistrationNumber>12346</ChipRegistrationNumber>
<DateOfBirth>
<date>1234-11-11T00:00:00</date>
</DateOfBirth>
<IsReserved>false</IsReserved>
<Name>Yoshi</Name>
<BadHabits>Scratches</BadHabits>
<Price>51</Price>
</Animal>

<Animal i:type="Dog">
<ChipRegistrationNumber>12347</ChipRegistrationNumber>
<DateOfBirth>
<date>1234-11-11T00:00:00</date>
</DateOfBirth>
<IsReserved>false</IsReserved>
<Name>Sonic</Name>
<LastWalkDate>
<date>1234-11-11T00:00:00</date>
</LastWalkDate>
<Price>200</Price>
</Animal>
</ArrayOfAnimal>

如何在
列表中解析XML文件中的所有猫和狗

这是与XML文件相关的类。 产生于

编辑:在“Price”元素附近有一个无效的XML:
51

[XmlRoot(ElementName=“DateOfBirth”,命名空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共类出生日期{
[XmlElement(ElementName=“日期”,命名空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共字符串日期{get;set;}
}
[XmlRoot(ElementName=“Animal”,名称空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公营动物{
[XmlElement(ElementName=“ChipRegistrationNumber”,命名空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共字符串ChipRegistrationNumber{get;set;}
[XmlElement(ElementName=“DateOfBirth”,命名空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共出生日期出生日期{get;set;}
[XmlElement(ElementName=“IsReserved”,命名空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共字符串被保留{get;set;}
[XmlElement(ElementName=“Name”,命名空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共字符串名称{get;set;}
[XmlElement(ElementName=“坏习惯”,命名空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共字符串坏习惯{get;set;}
[xmlement(ElementName=“Price”,命名空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共字符串Price{get;set;}
[XmlAttribute(AttributeName=“type”,命名空间=”http://www.w3.org/2001/XMLSchema-instance")]
公共字符串类型{get;set;}
[XmlElement(ElementName=“LastWalkDate”,命名空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共LastWalkDate LastWalkDate{get;set;}
}
[XmlRoot(ElementName=“lastwarkdate”,命名空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共类LastWalkDate{
[XmlElement(ElementName=“日期”,命名空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共字符串日期{get;set;}
}
[XmlRoot(ElementName=“ArrayOfAnimal”,名称空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共类ArrayOfAnimal{
[XmlElement(ElementName=“Animal”,命名空间=”http://schemas.datacontract.org/2004/07/AnimalShelter")]
公共列表{get;set;}
[XmlAttribute(AttributeName=“i”,命名空间=”http://www.w3.org/2000/xmlns/")]
公共字符串I{get;set;}
[xmldattribute(AttributeName=“xmlns”)]
公共字符串Xmlns{get;set;}
}

您可能可以使用以下内容:

using (FileStream f = new FileStream(<pathtoxml>, FileMode.Open, FileAccess.Read))
            {
                DataContractSerializer dcs = new DataContractSerializer(typeof(List<Animal>));
                XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(f, new XmlDictionaryReaderQuotas());

                List<Animal> listfromxml = (List<Animal>)dcs.ReadObject(reader);
            }
使用(FileStream f=newfilestream(,FileMode.Open,FileAccess.Read))
{
DataContractSerializer dcs=新的DataContractSerializer(类型(列表));
XmlDictionaryReader=XmlDictionaryReader.CreateTextReader(f,新的XmlDictionaryReaderQuotas());
List listfromxml=(List)dcs.ReadObject(reader);
}

正确,我的错。。编辑。
[XmlRoot(ElementName="DateOfBirth", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
    public class DateOfBirth {
        [XmlElement(ElementName="date", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
        public string Date { get; set; }
    }

    [XmlRoot(ElementName="Animal", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
    public class Animal {
        [XmlElement(ElementName="ChipRegistrationNumber", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
        public string ChipRegistrationNumber { get; set; }
        [XmlElement(ElementName="DateOfBirth", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
        public DateOfBirth DateOfBirth { get; set; }
        [XmlElement(ElementName="IsReserved", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
        public string IsReserved { get; set; }
        [XmlElement(ElementName="Name", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
        public string Name { get; set; }
        [XmlElement(ElementName="BadHabits", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
        public string BadHabits { get; set; }
        [XmlElement(ElementName="Price", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
        public string Price { get; set; }
        [XmlAttribute(AttributeName="type", Namespace="http://www.w3.org/2001/XMLSchema-instance")]
        public string Type { get; set; }
        [XmlElement(ElementName="LastWalkDate", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
        public LastWalkDate LastWalkDate { get; set; }
    }

    [XmlRoot(ElementName="LastWalkDate", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
    public class LastWalkDate {
        [XmlElement(ElementName="date", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
        public string Date { get; set; }
    }

    [XmlRoot(ElementName="ArrayOfAnimal", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
    public class ArrayOfAnimal {
        [XmlElement(ElementName="Animal", Namespace="http://schemas.datacontract.org/2004/07/AnimalShelter")]
        public List<Animal> Animal { get; set; }
        [XmlAttribute(AttributeName="i", Namespace="http://www.w3.org/2000/xmlns/")]
        public string I { get; set; }
        [XmlAttribute(AttributeName="xmlns")]
        public string Xmlns { get; set; }
    }
using (FileStream f = new FileStream(<pathtoxml>, FileMode.Open, FileAccess.Read))
            {
                DataContractSerializer dcs = new DataContractSerializer(typeof(List<Animal>));
                XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(f, new XmlDictionaryReaderQuotas());

                List<Animal> listfromxml = (List<Animal>)dcs.ReadObject(reader);
            }