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