序列化 class 自动向 XML 元素添加前缀

Serializing class add prefixes automatically to XML elements

我有以下class需要序列化:

[XmlRoot("Login", Namespace = "http://tempuri.org/Logon"), Serializable()]
    public class Login
    {
        [XmlElement("programCode")]
        public string ProgramCode { get; set; }
        [XmlElement("contactType")]
        public string ContactType { get; set; }
        [XmlElement("email")]
        public string Email { get; set; }
        [XmlElement("password")]
        public string Password { get; set; }
        [XmlElement("projectName")]
        public string ProjectName { get; set; }
}

当我序列化这个 class 时,我得到以下内容 XML:

<q1:Login xmlns:q1="http://tempuri.org/Logon"><q1:programCode>abc</q1:programCode><q1:contactType>P</q1:contactType><q1:email>ws@abc.com</q1:email><q1:password>abc</q1:password><q1:projectName>abc</q1:projectName></q1:Login>

我不知道前缀 q1 是从哪里生成的。我想要这样的 XML:

<Login xmlns="http://tempuri.org/Logon">
      <programCode>abc</programCode>
      <contactType>P</contactType>
      <email>ws@abc.com</email>
      <password>abc</password>
      <projectName>abc</projectName>
    </Login>

谁能帮我解决这个问题?谢谢。

更新: 序列化代码:

public string GetObjectInXML(object obj)
        {
            StringWriter sw = new StringWriter();
            StringBuilder sb = new StringBuilder(_soapEnvelope);
            XmlSerializer serializer = new XmlSerializer(obj.GetType());
            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            XmlWriterSettings settings = new XmlWriterSettings
            {
                OmitXmlDeclaration = true
            };
            XmlWriter writer = XmlWriter.Create(sw, settings);

            ns.Add(string.Empty, string.Empty);
            serializer.Serialize(writer, obj, ns);
            var str = sw.ToString();

            return str;
        }

现在这是一个 returns 字符串的方法,只是为了检查我的 XML 是否正确构建。

你能删除名字 space 吗?

    [XmlRoot("Login", Namespace = ""), Serializable()]
    public class Login { 
        [XmlElement("programCode")]
        public string ProgramCode { get; set; }
        [XmlElement("contactType")]
        public string ContactType { get; set; }
        [XmlElement("email")]
        public string Email { get; set; }
        [XmlElement("password")]
        public string Password { get; set; }
        [XmlElement("projectName")]
        public string ProjectName { get; set; }
    }
    public static string SerializeXml<T>(T value)
    {
        var settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;

        var namespaces = new XmlSerializerNamespaces();
        namespaces.Add("q1", "http://tempuri.org/Logon");

        var xmlserializer = new XmlSerializer(typeof(T));
        var stringWriter = new StringWriter();

        using (var writer = XmlWriter.Create(stringWriter, settings))
        {
            xmlserializer.Serialize(writer, value, namespaces);
            return stringWriter.ToString();
        }
    }
    public static void Main(string[] args)
    {
        var login = new Login();
        login.ContactType = "XMLType";
        login.Email = "x@x.com";

        var a = SerializeXml(login);
        Console.WriteLine(a);
        Console.ReadLine();
    }

结果

<Login xmlns:q1="http://tempuri.org/Logon">
    <contactType>XMLType</contactType>
    <email>x@x.com</email>
</Login>

XMLSerializer 支持提供默认名称空间,例如

string defaultNamespace = "http://tempuri.org/Logon";

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add(string.Empty, defaultNamespace);

XmlSerializer xs = new XmlSerializer(typeof(T), defaultNamespace);