下面的这段代码演示了如何使用DataContractSerializer序列化程序序列化对象。
代码中首先使用LINQ to SQL从数据库中读取编号为ALFKI的客户的记录,并返回一个Customer实体类对象。
然后使用DataContractSerializer序列化对象,并将生成的XML打印到控制台上。
此示例代码使用DataContractSerializer.WriterObject()这个方法。
NorthwindDataContext db = new NorthwindDataContext(@"C:/LINQ/Northwind.mdf");
Customer CustomerObject =
db.Customers.Where(C => C.CustomerID == "ALFKI").Single();
DataContractSerializer Serializer = new DataContractSerializer(typeof(Customer));
StringBuilder SBuilder = new StringBuilder();
XmlWriter Writer = XmlWriter.Create(SBuilder);
Serializer.WriteObject(Writer, CustomerObject);
Writer.Close();
string Xml = SBuilder.ToString();
Console.WriteLine(Xml);
如何使用DataContractSerializer序列化对象
最新推荐文章于 2025-06-10 09:05:45 发布