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

时间:2015-10-20 09:25:24

标签: c# xml winforms visual-studio-2013

这是我用来将数据从List写入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;
                }

这是我的XML文件的一个示例:

<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>

正如您所看到的,Cat有一个名为&#34; BadHabits&#34;的节点。和狗有一个不同的名为&#34; LastWalkDate&#34;。

&#34;目录&#34;和#34;狗&#34;继承自Animal类。

我已经看到很多将字符串解析为List的例子..这很可行..但我的列表有不同的类型,如自定义日期类

Animal的实例示例:

SimpleDate date1 = new SimpleDate(11, 11, 1234);
Animal cat1 = new Cat("12346", date1, "Yoshi", "Scratches");

如何在List<Animal>中解析XML文件中的所有猫狗?

2 个答案:

答案 0 :(得分:1)

这是与XML文件相关的类。 使用this tools生成

编辑:您在“价格”元素附近有一个无效的XML:<Price>51</rice>

[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; }
    }

答案 1 :(得分:1)

您可以使用以下内容:

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);
            }