c# - 如何使用 Linq 序列化为 xml

标签 c# linq

如何使用 Linq 将 College 实例序列化为 XML?

class College
{
    public string Name { get; set; }
    public string Address { get; set; }
    public List<Person> Persons { get; set; }
}


class Person
{
    public string Gender { get; set; }
    public string City { get; set; }
}

最佳答案

您不能使用 LINQ 进行序列化。您可以使用 XmlSerializer .

  XmlSerializer serializer = new XmlSerializer(typeof(College));

  // Create a FileStream to write with.
  Stream writer = new FileStream(filename, FileMode.Create);
  // Serialize the object, and close the TextWriter
  serializer.Serialize(writer, i);
  writer.Close();

关于c# - 如何使用 Linq 序列化为 xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9502194/

相关文章:

.net - Enumerable.Aggregate<TSource> 中出现 InvalidOperationException 的原因是什么?

c# - LINQ 等效于以下代码?

c# - 使用 LINQ 解析亚马逊商城 XML

c# - Unity Sprite 运行时的两个图像动画

c# - 寻找一种在 C# 中使用 float 的更简单方法

c# - Web API 服务 - 如何使服务器上的请求并发执行

linq - LINQ在哪里具有AND OR条件

c# - LINQ Except 将第一个集合的项目发送到 IEqualityComparer Equals 方法

c# - 来自浏览器的 WCF 方法调用返回 400 错误请求

c# - VS2013 错误 "Exception has been thrown by the target of an invocation"