去除命名空间:
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