在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi

参考
http://www.csharper.net/blog/serializing_without_the_namespace__xmlns__xmlns_xsd__xmlns_xsi_.aspx

可使用以下代码:

//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 ();
settings.OmitXmlDeclaration = true; // Remove the <?xml version="1.0" encoding="utf-8"?>

XmlWriter writer = XmlWriter.Create ("output_file_name.xml", settings);
posted @ 2011-12-03 20:25  therockthe  阅读(231)  评论(0)    收藏  举报