无法反序列化此xml的所有元素

时间:2014-11-04 08:10:13

标签: c# xml xml-serialization deserialization

此XML的少数元素不会反序列化,但也不会引发任何错误。

<?xml version="1.0" encoding="utf-8"?>
<TrialMWordsRecord xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MyList>
    <MWords>
      <Id>0</Id>
      <Name>ListMWords1</Name>
      <Type>LIST</Type>
      <MyOutput>true</MyOutput>
      <WordsElements>
        <Words>
          <Name>ListMWords1</Name>
          <Value>Apple</Value>
          <Type>STRING</Type>
        </Words>
        <Words>
          <Name>ListMWords1</Name>
          <Value>Mango</Value>
          <Type>STRING</Type>
        </Words>
        <Words>
          <Name>ListMWords1</Name>
          <Value>Chickoo</Value>
          <Type>STRING</Type>
        </Words>
      </WordsElements>
    </MWords>
    <MWords>
      <Id>1</Id>
      <Type>RANDOM</Type>
      <MyOutput>true</MyOutput>
      <WordsElements>
        <Name>Limit</Name>
        <Value>3,8</Value>
        <Type>NUMERIC</Type>
      </WordsElements>
    </MWords>
  </TrialMWordsList>
</MyListRecord>

以下是我的课程:

[Serializable()]
[XmlRootAttribute("MyListRecord")]
public class MyList
{

    [XmlArray("MyList")]
    public List<MWords> MWords
    {
        get;
        set;
    }

    public static MyList Deserialize()
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(MyList));
        TextReader textReader = new StreamReader(Application.StartupPath + "\\MyList.xml");

        MyList resultList = (MyList)deserializer.Deserialize(textReader);

        textReader.Close();

        return resultList;
    }       
}

[Serializable()]
public class MWords
{

    public int Id
    {
        get;
        set;
    }

    public MWordsType Type
    {
        get;
        set;
    }

    public bool MyOutput
    {
        get;
        set;
    }

    public string Requirement
    {
        get;
        set;
    }

    [XmlArrayItem("WordsElements")]
    public List<Words> WordList
    {
        get;
        set;
    }

    public static MWords Deserialize()
    {
        XmlSerializer deserializer = new XmlSerializer(typeof(MWords));
        TextReader textReader = new StreamReader(Application.StartupPath + "\\MWords.xml");
        MWords mwords = (MWords)deserializer.Deserialize(textReader);
        textReader.Close();
        return mwords;
    }
}

public class Words
{
    public string Value
    {
        get;
        set;
    }

    public TYPE Type
    {
        get;
        set;
    }

    public string Name
    {
        get;
        set;
    }
}

现在,当我反序列化此XML时,如果类型为LiST ,则WordList会更新,例如,这里WordList的计数为3,但如果 Type为RANDOM ,则WordList为0,实际上应为1而不是0。 真的不知道是什么原因。

2 个答案:

答案 0 :(得分:1)

RANDOM 类型的元素WordsElements的内容未包含在<Words></Words>中:

<MWords>
  <Id>1</Id>
  <Type>RANDOM</Type>
  <MyOutput>true</MyOutput>
  <WordsElements>
    <Words>
      <Name>Limit</Name>
      <Value>3,8</Value>
      <Type>NUMERIC</Type>
    </Words>
  </WordsElements>
</MWords>

答案 1 :(得分:1)

问题出在您的XML中。看看你的工作案例是什么样的:

<WordsElements>
  <Words>
    <Name>ListMWords1</Name>
    <Value>Apple</Value>
    <Type>STRING</Type>
  </Words>
  <Words>
    ...
  </Words>
  <Words>
    ...
  </Words>
</WordsElements>

现在将其与您的破案进行比较:

<WordsElements>
  <Name>Limit</Name>
  <Value>3,8</Value>
  <Type>NUMERIC</Type>
</WordsElements>

你没有Words元素 - 它应该是:

<WordsElements>
  <Words>
    <Name>Limit</Name>
    <Value>3,8</Value>
    <Type>NUMERIC</Type>
  </Words>
</WordsElements>

如果您无法更改XML,可能需要手动反序列化 - 这可能不会太难。

顺便说一句,你可能想要在一行上编写你自动实现的属性 - 这是一个 lot 更紧凑的写

public string Name { get; set; }

public string Name
{
    get;
    set;
}

......并且不难阅读,IMO。