gpt4 book ai didi

c# - 删除使用 DataContractSerializer 序列化的命名空间

转载 作者:行者123 更新时间:2023-12-03 09:29:54 25 4
gpt4 key购买 nike

我使用 DataContractSerializer 来序列化一个对象。这是我的代码:

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());
}
}

这是我的输出:

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

我现在需要删除 xmlns:i 和 xmlns:a 并设置 Indent = true;,我该怎么做?

最佳答案

首先,你需要用[DataContract(Namespace = "http://schemas.datacontract.org/2004/07/WcfWebService")]标记你所有的类声明每个类都应该在该命名空间中序列化。完成后,您还必须将每个或属性标记为 [DataMember] 以进行序列化。因为 DataContractSerializer 是选择加入的。

因此:

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

其次,至于缩进,您可以创建一个 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);
}
}

第三,关于删除标准命名空间 xmlns:i="http://www.w3.org/2001/XMLSchema-instance" 我不认为这可能直接与 DataContractSerializer。 (XmlSerializer 允许更多控制。)它是无害的,但您可以按照 Avoiding using the “http://www.w3.org/2001/XMLSchema-instance” namespace with .Net DataContractSerializer 中的说明手动删除它。 .

关于c# - 删除使用 DataContractSerializer 序列化的命名空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31901937/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com