码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-13 12:42:36      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:style   color   使用   for   io   代码   

可使用以下代码:

//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);

在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi,布布扣,bubuko.com

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

标签:style   color   使用   for   io   代码   

原文地址:http://www.cnblogs.com/GoogleGetZ/p/3837614.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!