在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
2014-07-11 13:37
477 查看
可使用以下代码:
此外,在评论中还提到了去除开头的<?xml version="1.0" encoding="utf-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"?>的方法:
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
- 在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
- 在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
- [zt]在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
- 在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
- 在XML序列化时去除默认命名空间xmlns:xsd和xmlns:xsi
- web.xml开头<web-app>中的xmlns、xsi、schemaLocation、xsd等,命名空间
- .Net 序列化(去除默认命名空间,添加编码)
- xml 序列化时, 去掉默认的命名空间
- 【转】C# XML序列化去掉XML默认的命名空间及声明头
- .Net 序列化(去除默认命名空间,添加编码)
- .Net 序列化(去除默认命名空间,添加编码)
- .NET(C#):使用XPath查询带有命名空间(有xmlns)的XML
- Jaxb解析xml(带命名空间)相关联的属性 "xsi:NamespaceSchemaLocation" 的前缀 "xsi" 未绑定。
- C# XML序列化去掉XML默认的命名空间及声明头
- 解析带有命名空间xmlns的xml文件 (XML解析中的namespace初探)
- XML之命名空间的作用(xmlns)
- .NET(C#):使用XPath查询带有命名空间(有xmlns)的XML
- 读取带有Xmlns命名空间的XML文件
- XML之命名空间的作用(xmlns)