在XML序列化時去除默認命名空間xmlns:xsd和xmlns:xsi


可使用以下代碼:

//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"?>的方法:

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"時,應該使用:
 XmlWriterSettings settings = new XmlWriterSettings ();
settings.Encoding = Encoding.UTF8;
 XmlWriter writer = XmlWriter.Create ("output_file_name.xml", settings);


免責聲明!

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



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