简体   繁体   English

删除使用DataContractSerializer序列化的名称空间

[英]Removing namespaces serializing with DataContractSerializer

I use the DataContractSerializer in order to serialize an object. 我使用DataContractSerializer来序列化对象。 Here is my code: 这是我的代码:

private string XmlSerial<T>(T instance)
{
    DataContractSerializer Serializer = new DataContractSerializer(typeof(T));
    using (MemoryStream memoryStream = new MemoryStream())
    {
        Serializer.WriteObject(memoryStream, instance);
        return Encoding.Default.GetString(memoryStream.ToArray());
    }
}

Here is my output: 这是我的输出:

<WS_IN_GetAccountCredit xmlns="http://schemas.datacontract.org/2004/07/WcfWebService" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <GetAccountCreditParams xmlns:a="http://schemas.datacontract.org/2004/07/RestConsumer">
    <a:Password>String content</a:Password>
    <a:UserName>String content</a:UserName>
  </GetAccountCreditParams>
  <WSIdentity xmlns:a="http://schemas.datacontract.org/2004/07/RestConsumer">
    <a:WS_PassWord>String content</a:WS_PassWord>
    <a:WS_UserName>String content</a:WS_UserName>
  </WSIdentity>
</WS_IN_GetAccountCredit>

I now need to remove xmlns:i and xmlns:a and also set the Indent = true; 我现在需要删除xmlns:i和xmlns:a并设置Indent = true; , how can I do it? , 我该怎么做?

Firstly, you need to mark all your classes with [DataContract(Namespace = "http://schemas.datacontract.org/2004/07/WcfWebService")] to declare that each class should be serialized in that namespace. 首先,您需要使用[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/WcfWebService")]标记所有类,以声明每个类都应在该命名空间中序列化。 Having done that, you must also mark each or property to be serialized with [DataMember] since DataContractSerializer is opt-in. 完成此操作后,由于还选择加入DataContractSerializer ,因此还必须使用[DataMember]标记每个要序列化的属性。

Thus: 从而:

[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/WcfWebService")]
public class WS_IN_GetAccountCredit
{
    [DataMember]
    public GetAccountCreditParams GetAccountCreditParams { get; set; }
    [DataMember]
    public WSIdentity WSIdentity { get; set; }
}

[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/WcfWebService")]
public class GetAccountCreditParams
{
    [DataMember]
    public string Password { get; set; }
    [DataMember]
    public string UserName { get; set; }
}

[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/WcfWebService")]
public class WSIdentity
{
    [DataMember]
    public string WS_PassWord { get; set; }
    [DataMember]
    public string WS_UserName { get; set; }
}

Secondly, as for indentation, you can create an XmlWriterSettings with the desired indentation, then create an XmlWriter from it and use it for serialization, as in the following extension methods: 其次,进行缩进,你可以创建一个XmlWriterSettings与期望的压痕,然后创建一个XmlWriter从它和使用它的系列化,如下面的扩展方法:

public static class DataContractSerializerHelper
{
    public static string GetXml<T>(T obj, DataContractSerializer serializer)
    {
        using (var textWriter = new StringWriter())
        {
            var settings = new XmlWriterSettings { Indent = true, IndentChars = "    " };
            using (var xmlWriter = XmlWriter.Create(textWriter, settings))
            {
                serializer.WriteObject(xmlWriter, obj);
            }
            return textWriter.ToString();
        }
    }

    public static string GetXml<T>(T obj)
    {
        var serializer = new DataContractSerializer(typeof(T));
        return GetXml(obj, serializer);
    }
}

Thirdly, as for removing the standard namespace xmlns:i="http://www.w3.org/2001/XMLSchema-instance" I don't believe that is possibly directly with DataContractSerializer . 第三,关于删除标准名称空间xmlns:i="http://www.w3.org/2001/XMLSchema-instance"我不认为可能直接与DataContractSerializer ( XmlSerializer allows more control.) It's harmless, but you could manually remove it following the instructions in Avoiding using the “http://www.w3.org/2001/XMLSchema-instance” namespace with .Net DataContractSerializer . XmlSerializer可以进行更多控制。)它无害,但是您可以按照避免将.http://www.w3.org/2001/XMLSchema-instance命名空间与.Net DataContractSerializer一起使用中的说明进行手动删除。

[DataContract(Namespace = "")]

on top of each class makes it a lot nicer. 在每堂课的顶部,它变得更好了。 It removes the datacontract namespaces and the ugly node prefixes. 它删除了datacontract命名空间和丑陋的节点前缀。 However, the standard namespace stays. 但是,将保留标准名称空间。 That was ok for my case. 我的情况还可以。

Before: 之前:

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://schemas.datacontract.org/2004/07/MyClassname">
  <prop1>true</prop1>
  <prop2 xmlns:d2p1="http://schemas.datacontract.org/2004/07/MySubclassname">
    <d2p1:sub>true</d2p1:sub>
  </prop2>
</root>

After: 后:

<?xml version="1.0" encoding="utf-8"?>
<root xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <prop1>true</prop1>
  <prop2>
    <sub>true</sub>
  </prop2>
</root>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM