c# – 序列化包含List的列表

前端之家收集整理的这篇文章主要介绍了c# – 序列化包含List的列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图序列化包含非系统类型的列表.

下面是我的序列化代码,它在顶层工作正常.并返回一个有效的XmlDocument,但似乎不包含内部列表中的任何内容.

我环顾网络 – 并且在SO附近 – 但似乎找不到任何东西!

任何帮助非常感谢.

码:

public static XmlDocument SerializeToXML<T>(List<T> list,string rootElement)
{
    XmlAttributeOverrides overrides = new XmlAttributeOverrides();
    XmlAttributes attr = new XmlAttributes();
    attr.XmlRoot = new XmlRootAttribute(rootElement);
    overrides.Add(typeof(List<T>),attr);

    XmlDocument xmlDoc = new XmlDocument();
    XPathNavigator nav = xmlDoc.CreateNavigator();
    using (XmlWriter writer = nav.AppendChild())
    {
        XmlSerializer ser = new XmlSerializer(typeof(List<T>),overrides);

        ser.Serialize(writer,list);
    }
    return xmlDoc;
}

用于测试的代码

[TestFixture]
public class BaseTesting
{
    [Test]
    public void test()
    {
        List<ListTestClass> list = new List<ListTestClass>();

        for (int i = 0; i < 20; i++)
        {
            list.Add(new ListTestClass() { intProp = 1,stringProp = "string1",dtProp = DateTime.Now });
        }

        XmlDocument doc = Beyond.Base.Util.XMLUtils.SerializeToXML<ListTestClass>(list,"root");
    }
}

public class ListTestClass
{
    public int intProp { get; set; }

    public string stringProp { get; set; }

    public DateTime dtProp { get; set; }

    [XmlElement("Inner",typeof(InnerListTestClass))]
    public InnerListTestClass inner { get { return new InnerListTestClass() { intProp = 1,dtProp = DateTime.Now }; } }
}

public class InnerListTestClass
{
    public int intProp { get; set; }

    public string stringProp { get; set; }

    public DateTime dtProp { get; set; }
}

XML输出

<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
  <ListTestClass>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T09:43:35.1017998+01:00</dtProp>
  </ListTestClass>
</root>

解决方法

内部属性必须具有setter才能进行序列化.

如果你改成它

public InnerListTestClass inner { get; set; }

它将按照您的预期进行序列化.

<ListTestClass>
<intProp>1</intProp>
<stringProp>string1</stringProp>
<dtProp>2011-06-07T01:57:07.1200742-07:00</dtProp>
<Inner>
    <intProp>1</intProp>
    <stringProp>string1</stringProp>
    <dtProp>2011-06-07T01:57:07.1210743-07:00</dtProp>
</Inner>
</ListTestClass>
原文链接:https://www.f2er.com/csharp/99547.html

猜你在找的C#相关文章