1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//准备序化列对象 XmlSerializer xs = new XmlSerializer(obj.GetType()); MemoryStream ms = new MemoryStream(); //设置序序化XML格式 XmlWriterSettings xws = new XmlWriterSettings(); xws.Indent = true; xws.OmitXmlDeclaration = true; xws.Encoding = Encoding.UTF8; XmlWriter xtw = XmlTextWriter.Create(ms, xws); //去掉要结点的 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" 属性 XmlSerializerNamespaces _namespaces = new XmlSerializerNamespaces( new XmlQualifiedName[] { new XmlQualifiedName(string.Empty, "aa") }); xs.Serialize(xtw, obj,_namespaces); ms.Position = 0; xmlDoc = new XmlDocument(); xmlDoc.Load(ms); //给文档添加<?xml version="1.0" encoding="utf-8"?> XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null); xmlDoc.InsertBefore(xmlDecl, xmlDoc.DocumentElement); |
from:https://blog.csdn.net/a5251199/article/details/106546967
View Details
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; namespace object2xml { public static class XmlExtension { public static string Serialize<T>(this T value) { if (value == null) return string.Empty; var xmlserializer = new XmlSerializer(typeof(T)); using (StringWriter stringWriter = new StringWriter()) { using (var writer = XmlWriter.Create(stringWriter, new XmlWriterSettings { Indent = true })) { xmlserializer.Serialize(writer, value); return stringWriter.ToString(); } } } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace object2xml { public class Persion { public string Name { get; set; public int Age { get; set; } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; using System.Xml.Serialization; namespace object2xml { class Program { static void Main(string[] args) { Persion p = new Persion(); p.Name = "张三"; p.Age = 18; var xml = p.Serialize<Persion>(); Console.WriteLine(xml); } } } |
from:https://blog.csdn.net/justgono/article/details/45887619
View Details去除命名空间:
1 2 3 4 5 6 7 8 |
//Create our own namespaces for the output XmlSerializerNamespaces ns = new XmlSerializerNamespaces (); //Add an empty namespace and empty value ns.Add ("", ""); //Create the serializer XmlSerializer slz = new XmlSerializer (someType); //Serialize the object with our own namespaces (notice the overload) slz.Serialize (myXmlTextWriter, someObject, ns); |
此外,在评论中还提到了去除开头的<?xml version="1.0" encoding="utf-8"?>的方法:
1 2 3 4 |
XmlWriterSettings settings = new XmlWriterSettings (); // Remove the <?xml version="1.0" encoding="utf-8"?> settings.OmitXmlDeclaration = true; XmlWriter writer = XmlWriter.Create ("output_file_name.xml", settings); |
另外,如果出现开头没有encoding="utf-8"时,应该使用:
1 2 3 |
XmlWriterSettings settings = new XmlWriterSettings (); settings.Encoding = Encoding.UTF8; XmlWriter writer = XmlWriter.Create ("output_file_name.xml", settings); |
参考文献: 1.在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi from:https://www.swack.cn/wiki/001559619644599d7563755fc544a19aebbb4eaf0dcb274000/0015889968933656748bbc3214e4f04a714c163b9227520000
View Details