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

本文关键字:元素 xml 反序列化 | 更新日期: 2023-09-27 18:29:11

此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时,如果Type是LiST,则WordList会更新,例如,此处WordList的计数将为3,但如果Type是RANDOM则WordList为0,实际上应该是1而不是0。真的不知道是什么原因。

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

问题出在您的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,则可能需要手动反序列化它,这可能不会太难。

顺便说一句,你可能想在一行上写下你自动实现的属性——写更紧凑

public string Name { get; set; }

public string Name
{
    get;
    set;
}

读起来也不难,IMO.

类型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>
相关文章: