如何在没有架构信息的情况下将c#对象序列化为xml?

这就是我做的:

Serializable类:

[Serializable()] public class Ticket { public string CitationNumber { get; set; } public decimal Amount { get; set; } } 

然后将模型序列化为xml:

 var model = cart.Citations .Select(c => new Ticket(c.Number, c.Amount)).ToList(); var serializer = new XmlSerializer(typeof (List)); var sw = new StringWriter(); serializer.Serialize(sw, model); return sw.ToString(); 

输出sw.ToString()就像

    00092844 20   

有没有办法自定义Serialize()输出以删除那些架构信息,如: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"

如何将根元素ArrayOfTicket更改为其他内容?

我能控制那些输出吗?

你需要一些xml技巧……

 var serializer = new XmlSerializer(typeof(List)); var ns = new XmlSerializerNamespaces(); ns.Add("", ""); var sw = new StringWriter(); var xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings() { OmitXmlDeclaration = true }); serializer.Serialize(xmlWriter, model, ns); string xml = sw.ToString(); 

输出:

   a 1   b 2   

PS:我在XmlWriterSettings添加了Indent = true来获得上述输出