序列化类自动为XML元素添加前缀

时间:2017-12-12 09:02:16

标签: c# xml-serialization

我有以下需要序列化的类:

[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; }
}

当我序列化这个类时,我获得了以下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;
        }

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

2 个答案:

答案 0 :(得分:3)

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

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

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

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

答案 1 :(得分:0)

可以删除名称空间吗?

    [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>