如何反序列化xml文件

时间:2015-05-18 14:58:48

标签: c# xml windows-runtime

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

有人请指导我如何设计我的课程以反序列化。

这是我目前的班级设计

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

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

4 个答案:

答案 0 :(得分:3)

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

由于您未在问题中包含Xml文档声明,因此我不确定countries集合是否是文档的根节点。但是我们假设您的整个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);
}

答案 1 :(得分:2)

Windows 8应用程序没有Seri​​alizable属性,您必须使用DataContractAttributeDataMemberAttribute并装饰您的模型类,如下所示:

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

然后你可以序列化这个类,这里​​是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的示例:

https://stackoverflow.com/a/17055641/351383

答案 2 :(得分:-1)

序列化 - 反序列化类的第一步是使用Serializable属性

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

答案 3 :(得分:-1)

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

[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