【问题标题】:C# Serialize a class with a List data memberC# 用 List 数据成员序列化一个类
【发布时间】:2011-03-31 18:46:54
【问题描述】:

我有这个 c# 类:

public class Test
{
    public Test() { }

    public IList<int> list = new List<int>();
}

然后我有这个代码:

        Test t = new Test();
        t.list.Add(1);
        t.list.Add(2);

        IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication();
        StringWriter sw = new StringWriter();
        XmlSerializer xml = new XmlSerializer(t.GetType());
        xml.Serialize(sw, t);

当我查看 sw 的输出时,它是这样的:

<?xml version="1.0" encoding="utf-16"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" />

我添加到列表成员变量中的值 1,2 不显示。

  1. 那么我该如何解决这个问题呢?我将列表设为属性,但它似乎仍然不起作用。
  2. 我这里用的是xml序列化,还有其他的序列化器吗?
  3. 我想要表现!这是最好的方法吗?

--------------- 下面更新 ----------- --

所以我要序列化的实际类是这样的:

public class RoutingResult
    {
        public float lengthInMeters { get; set; }
        public float durationInSeconds { get; set; }

        public string Name { get; set; }

        public double travelTime
        {
            get
            {
                TimeSpan timeSpan = TimeSpan.FromSeconds(durationInSeconds);
                return timeSpan.TotalMinutes;
            }
        }

        public float totalWalkingDistance
        {
            get
            {
                float totalWalkingLengthInMeters = 0;
                foreach (RoutingLeg leg in Legs)
                {
                    if (leg.type == RoutingLeg.TransportType.Walk)
                    {
                        totalWalkingLengthInMeters += leg.lengthInMeters;
                    }
                }

                return (float)(totalWalkingLengthInMeters / 1000);
            }
        }

        public IList<RoutingLeg> Legs { get; set; } // this is a property! isnit it?
        public IList<int> test{get;set;} // test ...

        public RoutingResult()
        {
            Legs = new List<RoutingLeg>();
            test = new List<int>(); //test
            test.Add(1);
            test.Add(2);
            Name = new Random().Next().ToString(); // for test
        }
    }

但是序列化器产生的XML是这样的:

<RoutingResult>
  <lengthInMeters>9800.118</lengthInMeters>
  <durationInSeconds>1440</durationInSeconds>
  <Name>630104750</Name>
</RoutingResult>

???

它忽略了这两个列表?

【问题讨论】:

  • 可能XmlSerializerIList&lt;&gt; 有问题,如果您改为重新定义为List&lt;&gt; 会怎样?

标签: c# serialization xml-serialization


【解决方案1】:

1)您的list 是一个字段,而不是一个属性,XmlSerializer 只能使用属性,试试这个:

public class Test
{    
    public Test() { IntList = new List<int>() }    
    public IList<int> IntList { get; set; }
}

2)还有其他的序列化选项,Binary 是主要的另一个,尽管JSON 也有一个。

3) 二进制可能是最高效的方式,因为它通常是直接的内存转储,并且输出文件将是最小的。

【讨论】:

  • XmlSerializer 也不适用于 IList,所以我将成员变量更改为 List。然后就可以了。
【解决方案2】:

list 不是属性。将其更改为公开可见的属性,并且应该将其拾取。

【讨论】:

    【解决方案3】:

    我发现如果我使用 IList,XmlSerializer 不起作用,所以我将其更改为 List,这使它起作用。正如 Nate 也提到的那样。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-04
      • 2017-03-30
      • 2012-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多