【问题标题】:Read XML file parse it into a class list读取 XML 文件将其解析为类列表
【发布时间】:2015-10-20 09:25:24
【问题描述】:

这是我用来将数据从 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 有一个名为“BadHabits”的节点,而 Dog 有一个名为“LastWalkDate”的不同节点。

“猫”和“狗”继承自动物类。

我见过很多将字符串解析为列表的示例。这很可行。但我的列表有不同的类型,例如自定义日期类

Animal 实例示例:

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

如何从List&lt;Animal&gt; 中的 XML 文件中解析所有猫和狗?

【问题讨论】:

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


    【解决方案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);
                }
    

    【讨论】:

      【解决方案2】:

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

      编辑:“价格”元素附近的 XML 无效:&lt;Price&gt;51&lt;/rice&gt;

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

      【讨论】:

      • 正确,我的错……已编辑。
      猜你喜欢
      • 2015-05-03
      • 1970-01-01
      • 2014-12-25
      • 2013-08-16
      • 2015-04-13
      • 2017-11-07
      • 1970-01-01
      • 2011-04-01
      • 2012-11-22
      相关资源
      最近更新 更多