如何反序列化 xml 文件

How to deserialize a xml file

<countries>
    <country code="AF" iso="4">Afghanistan</country>
    <country code="AL" iso="8">Albania</country>
    <country code="DZ" iso="12">Algeria</country>
    <country code="AS" iso="16">American Samoa</country>
    <country code="AD" iso="20">Andorra</country>
    <country code="AO" iso="24">Angola</country>
    <country code="AI" iso="660">Anguilla</country>
    <country code="AQ" iso="10">Antarctica</country>
    <country code="AG" iso="28">Antigua And Barbuda</country>
    <country code="AR" iso="32">Argentina</country>
    <country code="AM" iso="51">Armenia</country>
    <country code="AW" iso="533">Aruba</country>
    <country code="AU" iso="36">Australia</country>
    <country code="AT" iso="40">Austria</country>
    <country code="AZ" iso="31">Azerbaijan</country>
    <country code="BS" iso="44">Bahamas</country>
    <country code="BH" iso="48">Bahrain</country>
    <country code="BD" iso="50">Bangladesh</country>
    <country code="BB" iso="52">Barbados</country>
    <country code="BY" iso="112">Belarus</country>
    <country code="BE" iso="56">Belgium</country>
    <country code="BZ" iso="84">Belize</country>
    <country code="BJ" iso="204">Benin</country>
    <country code="BM" iso="60">Bermuda</country>
    <country code="BT" iso="64">Bhutan</country>
    <country code="BO" iso="68">Bolivia</country>
    <country code="BA" iso="70">Bosnia And Herzegovina</country>
    <country code="BW" iso="72">Botswana</country>
    <country code="BV" iso="74">Bouvet Island</country>
    <country code="BR" iso="76">Brazil</country>
    <country code="IO" iso="86">British Indian Ocean Territory</country>
    <country code="BN" iso="96">Brunei Darussalam</country>
    <country code="BG" iso="100">Bulgaria</country>
    <country code="BF" iso="854">Burkina Faso</country>
    <country code="BI" iso="108">Burundi</country>
</countries>

有人请指导我如何设计我的 class 来反序列化这个 .

这是我目前的 class 设计

public class Country
{
    public string country { get; set; }
    public string code { get; set; }
    public int iso { get; set; }
}

但这似乎不起作用。请有人指导我。

序列化-反序列化 class 的第一步是用 Serializable 属性标记它

[Serializable]
public class Country
{
   public string country { get; set; }
   public string code { get; set; }
   public int iso { get; set; }
}

首先,正如奥斯卡所说。您必须将装饰器 Serializable 添加到您的 class.

[Serializable]
public class Country
{
   public string country { get; set; }
   public string code { get; set; }
   public int iso { get; set; }
}

然后您必须按照上述步骤将您的 xml 反序列化为对象:

private Country[] DeserializeCountries(string xmlPath)
{
    // Create an instance of the XmlSerializer specifying type and namespace.
    XmlSerializer serializer = new
    XmlSerializer(typeof(Country));

    // A FileStream is needed to read the XML document.
    FileStream fs = new FileStream(xmlPath, FileMode.Open);
    XmlReader reader = XmlReader.Create(fs);

    // Use the Deserialize method to restore the object's state.
    Country[] countries = (Country[])serializer.Deserialize(reader);
    fs.Close();

    return countries;
}

以上代码改编自Microsoft Documentation

与目前的其他答案相反,使用 Xml 序列化时不需要使用 Serializable 属性。但是您确实需要使用代码属性来装饰您的属性,这些代码属性描述了将从 Xml 文件的哪个部分获取值。

由于您没有在您的问题中包含 Xml 文档声明,我不确定国家集合是否是您文档的根节点。但是让我们假设您的整个 Xml 文档实际上是这样的:

<?xml version="1.0" encoding="utf-8" ?> 
<countries>
  <country code="AF" iso="4">Afghanistan</country>
  <country code="AL" iso="8">Albania</country>
  <country code="DZ" iso="12">Algeria</country>
</countries>

您需要将代码属性应用到您的 类,它描述了上述 Xml 如何映射到您的属性和对象。这些属性在 System.Xml 中定义。这就是您的情况下属性的外观:

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
...
[XmlRoot("countries",  Namespace="")]
public class countriesDocument
{
    [XmlElement("country")]
    public country[] countries { get; set; }
}

public class country
{
    [XmlText]
    public string name { get; set; }

    [XmlAttribute]
    public string code { get; set; }

    [XmlAttribute]
    public int iso { get; set; }
}

然后你可以使用如下代码反序列化文档:

var serializer = new XmlSerializer(typeof(countriesDocument));
countriesDocument document;
using (var reader = File.OpenText("countries.xml"))
{
    document = (countriesDocument)serializer.Deserialize(reader);
}

Windows 8 个应用程序没有 Serializable 属性,您必须使用 DataContractAttribute and DataMemberAttribute 并装饰您的模型 class,如下所示:

[DataContractAttribute]
public class Country
{
  [DataMemberAttribute]
  public string country { get; set; }
  [DataMemberAttribute]
  public string code { get; set; }
  [DataMemberAttribute]
  public int iso { get; set; }
}

然后你可以序列化这个class,这里是Json

的例子
public static string SerializeToJson(object instance)
{
  using (MemoryStream _Stream = new MemoryStream())
  {
    var _Serializer = new DataContractJsonSerializer(instance.GetType());
    _Serializer.WriteObject(_Stream, instance);
    _Stream.Position = 0;
    using (StreamReader _Reader = new StreamReader(_Stream))
    { return _Reader.ReadToEnd(); }
  }
}

请参阅此答案以获取 XML 的示例: