加载中…
个人资料
龙涎
龙涎
  • 博客等级:
  • 博客积分:0
  • 博客访问:2,586
  • 关注人气:128
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

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

(2011-03-18 17:21:58)
标签:

xml

序列化

去除

默认命名空间

xmlnsxsd

xmlnsxsi

it

分类: 平台技术

可使用以下代码:

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

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有