首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何反序列化xml文件

如何反序列化xml文件
EN

Stack Overflow用户
提问于 2015-05-18 14:58:48
回答 4查看 116关注 0票数 0
代码语言:javascript
复制
<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>

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

这是我目前的课程设计

代码语言:javascript
复制
public class Country
{
    public string country { get; set; }
    public string code { get; set; }
    public int iso { get; set; }
}

但这似乎行不通。请有人指点我。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2015-05-18 15:36:49

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

由于您的问题中没有包含Xml文档声明,所以我不确定countries集合是否是文档的根节点。但是,假设您的整个Xml文档实际上如下所示:

代码语言:javascript
复制
<?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中定义的。在您的情况下,这些属性可能是这样的:

代码语言:javascript
复制
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; }
}

然后,可以使用如下代码对文档进行反序列化:

代码语言:javascript
复制
var serializer = new XmlSerializer(typeof(countriesDocument));
countriesDocument document;
using (var reader = File.OpenText("countries.xml"))
{
    document = (countriesDocument)serializer.Deserialize(reader);
}
票数 3
EN

Stack Overflow用户

发布于 2015-05-18 15:40:32

Windows 8应用程序没有可序列化的属性,您必须使用DataContractAttributeDataMemberAttribute来装饰模型类,如下所示:

代码语言:javascript
复制
[DataContractAttribute]
public class Country
{
  [DataMemberAttribute]
  public string country { get; set; }
  [DataMemberAttribute]
  public string code { get; set; }
  [DataMemberAttribute]
  public int iso { get; set; }
}

然后您可以序列化这个类,下面是Json的示例

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-05-18 15:02:12

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

代码语言:javascript
复制
[Serializable]
public class Country
{
   public string country { get; set; }
   public string code { get; set; }
   public int iso { get; set; }
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30306578

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档