C# XML序列化 去掉xmlns:xsd和xmlns:xsi屬性


                //准備序化列對象
                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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM