使用“http://www.w3.org/2001/XMLSchema-instance”命名空间与.net的DataContractSerializer避免 [英] Avoiding using the “http://www.w3.org/2001/XMLSchema-instance” namespace with .Net DataContractSerializer

查看:214
本文介绍了使用“http://www.w3.org/2001/XMLSchema-instance”命名空间与.net的DataContractSerializer避免的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的,我在.NET 4.0中使用.NET的DataContractSerializer的转换为XML类。序列化工作正常,我可以解析XML,后来重新创建.NET对象没有任何困难。

I have a series of classes that I am converting to XML using .NET's DataContractSerializer in .NET 4.0. The serialisation is working fine, and I can parse the XML and recreate the .NET objects later without any difficulty.

然而,大多数数据成员的时候,并不需要

。 [数据成员(IsRequired = FALSE)。这对伟大的工程反序列化的XML,在这里你可以那么就错过了XML节点出文档,但序列化已有的对象到XML时,DataContractSerializer的坚持写出具有空值与属性例如节点的属性

However, most of the DataMember's are not required. [DataMember(IsRequired = false)]. This works great on de-serializing the XML, where you can then just miss the XML node out of the document, but when serializing an exisiting object into XML, the DataContractSerializer insists on writing out properties that have null values as nodes with an attribute e.g.

[DataContract(Name = "response", Namespace = "http://domain.com/name")]
public class MyResponseClass
{
    [DataMember(Name = "count", IsRequired = true, Order = 0)]
    public int Count { get; set; }

    [DataMember(Name = "info", IsRequired = false, Order = 1)]
    public InfoClass Info { get; set; }

    [DataMember(Name = "metadata", IsRequired = false, Order = 2)]
    public MetadataList Metadatas { get; set; }

}



可以从

can be serialised from

<response xmlns="http://domain.com/name">
    <count>4</count>
</response>



不过,如果我连载的对象,它创建:

However, if I serialise the object, it creates:

<response xmlns="http://domain.com/name" xmlns:i="http://www.w3.org/2001/XmlSchema-instance">
    <count>4</count>
    <info i:nil="true" />
    <metadata i:nil="true" />
</response>



有没有什么办法让DataContractSerializer的不写节点相反,当它有一个空值?

Is there any way to get the DataContractSerializer to not write the node instead, when it has a null value?

推荐答案

使用 EmitDefaultValue = FALSE 在XML中跳过的默认值:

Use EmitDefaultValue = false to skip default values in XML:

[DataContract(Name = "response", Namespace = "http://domain.com/name")]
public class MyResponseClass 
{
    [DataMember(Name = "count", IsRequired = true, Order = 0, EmitDefaultValue = false)]
    public int Count { get; set; }

    [DataMember(Name = "info", IsRequired = false, Order = 1, EmitDefaultValue = false)]
    public InfoClass Info { get; set; }

    [DataMember(Name = "metadata", IsRequired = false, Order = 2, EmitDefaultValue = false)]
    public MetadataList Metadatas { get; set; }
}



删除的xmlns:I =HTTP: //www.w3.org/2001/XmlSchema-instance你必须使用例如替换()像下面的例子

to remove xmlns:i="http://www.w3.org/2001/XmlSchema-instance" you have to use for example Replace() like in the following example

public void Write(string filePath, MyResponseClass myResponse)
{
    var serializer = new DataContractSerializer(typeof(MyResponseClass));

    var sb = new StringBuilder();
    using (var writer = new StringWriter(sb))
    using (var xmlWriter = XmlWriter.Create(writer))
    {
        serializer.WriteObject(xmlWriter, myResponse);
    }

    using (StreamWriter stream = new StreamWriter(filePath))
    {
        sb = sb.Replace(" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"", "");
        stream.Write(sb);
    }
}



与问候:)

with regards :)

这篇关于使用“http://www.w3.org/2001/XMLSchema-instance”命名空间与.net的DataContractSerializer避免的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆