首页 > 代码库 > 在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
在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);
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。