2

我有以下 XML

<getAvailability>
  <result>
    <arrival_date>2013-05-05</arrival_date>
    <block>
    <block_id>80884788</block_id>
    </block>
    <id>230802</id>
   </result>
   <result>
    <arrival_date>2013-05-05</arrival_date>
    <block>
    <block_id>419097</block_id>
    </block>
    <id>98121</id>
  </result>
</getAvailability>

如何反序列化它并将其放入 C# 中的类中?非常感谢。另外这里是我的代码;

XmlSerializer serializer = new XmlSerializer(typeof(getAvailability));
getAvailability output;

using (StringReader reader = new StringReader(xmlSource))
{
    output = (getAvailability)serializer.Deserialize(reader);
}

public class Result
{
    [XmlElement("block_id")]
    public string sBlockID { get; set; }

    [XmlElement("arrival_date")]
    public DateTime dArrivalDate { get; set; }

    [XmlElement("id")]
    public int iID{ get; set; }
}

[XmlRoot("getAvailability")]
public class getAvailability
{
     [XmlArray("result")]
     [XmlArrayItem("block", typeof(Result))]
     public Result[] Result { get; set; }
}

如果您需要任何进一步的信息,请告诉我。

4

1 回答 1

2

最简单的方法是按照下面的代码示例构建结果类,以便轻松地将其映射到您提供的 XML 示例。我添加了一些注释,提供有关某些方法的信息。

using System;
using System.IO;
using System.Xml.Serialization;

namespace Test
{
    public class Program
    {
        public static void Main(string[] args)
        {

            Result result1 = new Result
                                 {
                                     arrival_date = new DateTime(2013, 05, 05),
                                     block = new Result.Block { block_id = 80884788 },
                                     id = 230802
                                 };
            Result result2 = new Result
                                 {
                                     arrival_date = new DateTime(2013, 05, 05),
                                     block = new Result.Block { block_id = 419097 },
                                     id = 98121
                                 };
            Results results = new Results { result = new Result[2] };
            results.result[0] = result1;
            results.result[1] = result2;

            WriteSettingsAsXml("D:\\test.xml", typeof(Results), results, true);

            Results gA = (Results)ReadSettingsFromXml("D:\\test.xml", typeof(Results));
        }

        // This `Result` class below maps to a single result in the XML you provided. This class is used in the `Results` class to obtain the needed XML structure.   

        public class Result
        {
            public class Block
            {
                public Int32 block_id { get; set; }
            }

            public DateTime arrival_date { get; set; }
            public Block block { get; set; }
            public Int32 id { get; set; }
        }

        [Serializable()]
        [XmlRootAttribute("getAvailability", Namespace = "", DataType = "", IsNullable = false)]
        public class Results
        {
            [XmlElement("result")]
            public Result[] result { get; set; }
        }

        /// Library methods that saves/reads any passed/retrieved object into/from a xml file at specified location

        public static void WriteSettingsAsXml(string destinationPath, Type objectType, object objectValue, bool hideNamespaces)
        {
            XmlSerializer serializer = new XmlSerializer(objectType);
            using (TextWriter writer = new StreamWriter(destinationPath))
            {
                if (hideNamespaces)
                {
                    XmlSerializerNamespaces hiddenNamespaces = new XmlSerializerNamespaces();
                    hiddenNamespaces.Add("", "");
                    serializer.Serialize(writer, objectValue, hiddenNamespaces);
                }
                else
                    serializer.Serialize(writer, objectValue);
                writer.Close();
            }
        }
        public static object ReadSettingsFromXml(string xmlFilePath, Type objectType)
        {
            XmlSerializer serializer = new XmlSerializer(objectType);
            using (FileStream fileStream = new FileStream(xmlFilePath, FileMode.Open))
            {
                return serializer.Deserialize(fileStream);
            }
        }
    }
}
于 2013-05-03T19:19:00.983 回答