如何将List <T> pp序列化为XML?

时间:2019-11-07 05:13:51

标签: c# xml

如何将List<T> pp序列化为XML?

var pp = new List<T>();

if (reader.HasRows) 
{

    Console.WriteLine("{0} \t{1} \t{2}", reader.GetName(0), reader.GetName(1), reader.GetName(2));

    while (reader.Read())
    {
        var obj = new T();
        obj.S = reader.GetString(0);
        obj.S1 = reader.GetString(1);
        obj.S2 = reader.GetInt32(2);
        obj.S3 = reader.GetString(3);

        pp.Add(obj);

    }
}

如何将List<T> pp序列化为XML?

1 个答案:

答案 0 :(得分:0)

下面的解决方案就是代码

XmlSerializer xsGList = new XmlSerializer(typeof(List<T>),
                               new XmlRootAttribute("Ts"));
 var subReq = new List<T>(); // assign the value
 subReq=pp; 
 var xml = "";

 using(var stream = new StringWriter())
 {
  using(XmlWriter writer = XmlWriter.Create(stream))
  {
     xsGList.Serialize(writer, subReq);
     xml = stream.ToString(); // Your XML as string
  }
 }