这是一个hack-ish的方法,而不必将整个输出字符串加载到XmlDocument中:
原文链接:https://www.f2er.com/xml/292263.htmlusing System; using System.Text; using System.Xml; using System.Xml.Serialization; public class Example { public String Name { get; set; } static void Main() { Example example = new Example { Name = "Foo" }; XmlSerializer serializer = new XmlSerializer(typeof(Example)); XmlSerializerNamespaces emptyNamespace = new XmlSerializerNamespaces(); emptyNamespace.Add(String.Empty,String.Empty); StringBuilder output = new StringBuilder(); XmlWriter writer = XmlWriter.Create(output,new XmlWriterSettings { OmitXmlDeclaration = true }); serializer.Serialize(writer,example,emptyNamespace); Console.WriteLine(output.ToString()); } }