您没有提供大量细节,但您可以使用XmlSerializer类。如果您只想输出到字符串,则可以使用字符串编写器。
来自 MSDN 的代码示例:
XmlSerializer serializer =
new XmlSerializer(typeof(OrderedItem));
// Create an instance of the class to be serialized.
OrderedItem i = new OrderedItem();
// Set the public property values.
i.ItemName = "Widget";
i.Description = "Regular Widget";
i.Quantity = 10;
i.UnitPrice = (decimal) 2.30;
// Writing the document requires a TextWriter.
TextWriter writer = new StreamWriter(filename);
// Serialize the object, and close the TextWriter.
serializer.Serialize(writer, i);
writer.Close();