如何删除导出的xml中的xmlns:xsd=

在 C# 中使用 XmlSerializer 进行 XML 序列化时,默认会自动添加以下两个命名空间声明:

xml 复制代码
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"

这些命名空间是为了支持 XML Schema 实例(XSI)和 XML Schema 定义(XSD)的验证。但在某些场景下,你可能希望 去掉这些命名空间声明,以满足特定的格式要求或与外部系统兼容。


✅ 解决方案:删除 xmlns:xsdxmlns:xsi

方法一:使用 XmlAttributeOverridesXmlSerializerNamespaces

这是最推荐、最灵活的方式,适用于大多数 XML 序列化场景。

✅ 示例代码

csharp 复制代码
using System;
using System.IO;
using System.Xml.Serialization;

public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}

class Program
{
    static void Main()
    {
        Person person = new Person { Name = "Alice", Age = 25 };

        // 创建 XmlAttributeOverrides,覆盖默认的命名空间
        XmlAttributeOverrides overrides = new XmlAttributeOverrides();
        XmlAttributes attributes = new XmlAttributes();

        // 设置 XmlRootAttribute 并指定命名空间为空
        XmlRootAttribute root = new XmlRootAttribute("Person");
        root.Namespace = ""; // 设置为空字符串

        attributes.XmlRoot = root;
        overrides.Add(typeof(Person), attributes);

        // 创建 XmlSerializer,并使用空的命名空间集合
        XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
        namespaces.Add(string.Empty, string.Empty); // 忽略默认命名空间

        XmlSerializer serializer = new XmlSerializer(typeof(Person), overrides);

        using (StringWriter writer = new StringWriter())
        {
            serializer.Serialize(writer, person, namespaces);
            Console.WriteLine(writer.ToString());
        }
    }
}

输出结果

xml 复制代码
<Person>
  <Name>Alice</Name>
  <Age>25</Age>
</Person>

方法二:使用 XmlTextWriter 手动控制输出格式(可选)

如果你需要更细粒度的控制(例如删除特定属性),可以使用 XmlTextWriter 并在写入时忽略命名空间。

示例代码(使用 XmlTextWriter)
csharp 复制代码
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

class Program
{
    static void Main()
    {
        Person person = new Person { Name = "Alice", Age = 25 };

        XmlSerializer serializer = new XmlSerializer(typeof(Person));

        using (StringWriter stringWriter = new StringWriter())
        using (XmlTextWriter xmlWriter = new XmlTextWriter(stringWriter))
        {
            xmlWriter.Formatting = Formatting.Indented;

            // 禁用命名空间处理
            XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
            namespaces.Add(string.Empty, string.Empty);

            serializer.Serialize(xmlWriter, person, namespaces);
            Console.WriteLine(stringWriter.ToString());
        }
    }
}

📌 注意事项

项目 说明
命名空间是否必须 如果你的 XML 需要被外部系统验证或解析为特定的 Schema,删除 xsdxsi 可能会导致兼容性问题。
性能影响 使用 XmlAttributeOverridesXmlSerializerNamespaces 对性能影响较小,推荐使用。
是否影响反序列化 如果你使用相同的类结构进行反序列化,即使没有命名空间声明,XmlSerializer 也能正确识别。
是否适用于嵌套类/集合类 是的,上述方法适用于包含嵌套类、集合类等复杂结构的序列化。

🧩 高级用法:序列化集合类并去除命名空间

csharp 复制代码
[XmlRoot("People")]
public class People
{
    [XmlElement("Person")]
    public List<Person> Persons { get; set; }
}

// 序列化时使用相同的方法,添加 XmlAttributeOverrides

✅ 总结

方法 说明 推荐程度
XmlAttributeOverrides + XmlSerializerNamespaces 推荐方式,控制命名空间最灵活 ⭐⭐⭐⭐⭐
XmlTextWriter + 手动控制 可选方式,适用于需要更细粒度控制的场景 ⭐⭐⭐
直接修改 XML 字符串 不推荐,容易出错且不可靠

通过上述方法,你可以轻松地 在 XML 序列化时去除 xmlns:xsdxmlns:xsi 命名空间声明 。推荐使用 XmlAttributeOverridesXmlSerializerNamespaces 的组合,这是最稳定、最推荐的方式,适用于大多数 XML 序列化场景,包括嵌套类、集合类等复杂结构。

相关推荐
方芯半导体8 小时前
EtherCAT “通信 + 控制“ 的全国产化控制方案,ESC芯片(FCE1323)与国产MCU芯片功能板解析
xml·网络·单片机·嵌入式硬件·网络协议·机器人·自动化
好好研究9 小时前
总结SSM设置欢迎页的方式
xml·java·后端·mvc
R-sz2 天前
mybatis的XML,如何多值匹配,支持单值(=)和多值(IN)查询
xml·mybatis
方芯半导体2 天前
EtherCAT从站控制器芯片(FCE1353)与MCU(STM32H743)功能板解析!
xml·stm32·单片机·嵌入式硬件·物联网·自动化
码农娟2 天前
Hutool XML工具-XmlUtil的使用
xml·java
码农娟3 天前
Hutool XML工具-XmlUtil遇到标签问题
xml
草履虫建模4 天前
A02 Maven 基础配置:本地仓库、镜像、项目编码与常见问题(IDEA 实战)
xml·java·spring boot·spring·maven·intellij-idea·idea
Dawndddddd4 天前
XXE(XML外部实体注入)漏洞
xml·xxe
嵌入式老表4 天前
ISO15118-2 解读4 —— XML、EXI、签名
xml
学海无涯书山有路5 天前
Android LiveData + MVVM 新手入门教程(基于 XML+Java)
android·xml·java