【问题标题】:How to deserialize this xml using List (not array)如何使用 List(不是数组)反序列化此 xml
【发布时间】:2014-08-07 16:41:54
【问题描述】:

我正在尝试使用 c# 在 SILVERLIGHT 中进行反序列化。在我使用 XmlArray 之前它正在工作,但我想使用 List 代替,因为 我已经使用 List 完成了前面的部分(所以当我只使用数组时看起来很奇怪)。

我的xml是这样的:

<ps>
    <disable_others>
        <disable_other>
            <disable_value>1</disable_value>
            <to_disable>skew</to_disable>
            <to_disable>skew_side</to_disable>
        </disable_other>
        <disable_other>
            <disable_value>0</disable_value>
            <to_disable>automodel</to_disable>
        </disable_other>
    </disable_others>
<ps>

我尝试反序列化它是:

[XmlRoot(ElementName = "ps")]
    public class ps
    {
      [XmlArray("disable_others")]
      [XmlArrayItem("disable_other", IsNullable = false)]  
      public List<string> Disable_Others { get; set; } 

      /* I know it can be done using the below but i have to use List not array:
              [XmlArray("disable_others")]
        [XmlArrayItem("disable_other", IsNullable = false)]
        public Disable_Other[] Disable_Others { get; set; } */



    }


[XmlRoot(ElementName = "disable_Others")]
    public class Disable_Others
    {
        [XmlElement("disable_other")]
        public List<Disable_Other> Disable_Other { get; set; }

    }

[XmlRoot(ElementName = "Disable_Other")]
    public class Disable_Other
    {
        [XmlElement("disablingitem")]
        public int DisablingItem { get; set; }

        [XmlElement("to_disable")]
        public string[] To_Disable { get; set; }

        [XmlElement("disable_value")]
        public byte Disable_Value { get; set; }

    }

有人可以帮我实现使用 List 而不是 array(XmlArray) 的目标吗?

【问题讨论】:

  • 如果你已经在一个数组上工作了,你不能在数组上使用Enumerable.ToList() 吗? (还是说效率太低了?)
  • @MatthewWatson 不,我必须手动完成。
  • 您可以使用 xmlarray...与列表字段...并且您必须将您的类标记为 [serializable]。
  • @terrybozzio 你的意思是这样的: [XmlArray("disable_others")] [XmlArrayItem("disable_other", IsNullable = false)] public List Disable_Others { get;放; }
  • 更像:[XmlArray("disable_others")] [XmlArrayItem(typeof(disable_other), IsNullable = false)] public List Disable_Others { get;放; }.

标签: c# serialization xml-serialization deserialization xml-deserialization


【解决方案1】:

您的 XML 格式应如下所示:

<?xml version="1.0"?>
<ps xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <disable_others>
    <Disable_Other>
      <disablingitem>0</disablingitem>
      <to_disable>
        <string>skew</string>
        <string>skew_side</string>
      </to_disable>
      <disable_value>1</disable_value>
    </Disable_Other>
    <Disable_Other>
      <disablingitem>0</disablingitem>
      <to_disable>
        <string>automodel</string>
      </to_disable>
      <disable_value>0</disable_value>
    </Disable_Other>
  </disable_others>
</ps>

在此之后,您的课程应该如下:

    [Serializable]
    [XmlRoot(ElementName = "ps")]
    public class ps
    {
        [XmlArray("disable_others")]
        [XmlArrayItem(typeof(Disable_Other), IsNullable = false)]
        public List<Disable_Other> Disable_Others { get; set; }

        public ps()
        {
            Disable_Others = new List<Disable_Other>();
        }

    }

    [Serializable]
    [XmlRoot("disable_other")]
    public class Disable_Other
    {
        [XmlElement("disablingitem")]
        public int DisablingItem { get; set; }

        //[XmlElement("to_disable")]
        [XmlArray("to_disable")]
        public string[] To_Disable { get; set; }

        [XmlElement("disable_value")]
        public byte Disable_Value { get; set; }

    }

并在您的代码中像这样反序列化:

ps p;
XmlSerializer serializer = new XmlSerializer(typeof(ps));
using (StreamReader reader = new StreamReader("test1.xml"))
{
    //basic exception handling...
    try
    {
        p = (ps)serializer.Deserialize(reader);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
 }

【讨论】:

  • 抱歉,我无法访问“skew”和“skew_side”。为什么我需要数组来访问 Disable_Other[index] 中的数据?我正在尝试执行“object1.Disable_Others[0].To_Disable”,因为它对里面的所有内容都显示 0 和 null。
  • 是否必须使用 [Serializable] 因为在我没有使用它之前它正在工作(当我像这样工作时 public Disable_Other[] Disable_Others { get; set; })即使没有 [Serializable ].
  • 您的 xml 格式有 2 个 to_disable 值,在您的代码中 To_Disable 是一个数组,在 xml 中它应该像我的示例一样,请尝试一下,制作一个 xml 文件并粘贴我的示例 xml,然后像我一样创建 2 个类,并在您的调用代码中放置我的反序列化示例,您将看到一切正常...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-12
  • 1970-01-01
  • 2020-01-31
  • 2012-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多