使用xsi:nil =“ true”反序列化XML元素。在C#中 [英] Deserialize XML element with xsi:nil="true" in C#

查看:417
本文介绍了使用xsi:nil =“ true”反序列化XML元素。在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用C#中的 XmlSerializer 反序列化XML文件。

I'm trying to deserialize a XML file with XmlSerializer in C#.

后面的目标类是使用xsd实用程序自动生成的。

The destination class that follows was automatically generated using the xsd utility.

    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = true)]
    public partial class location
    {

        private string cityField;

        private string countryField;

        private string stateField;

        private string textField;

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string city
        {
            get
            {
                return this.cityField;
            }
            set
            {
                this.cityField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string country
        {
            get
            {
                return this.countryField;
            }
            set
            {
                this.countryField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string state
        {
            get
            {
                return this.stateField;
            }
            set
            {
                this.stateField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTextAttribute()]
        public string Text
        {
            get
            {
                return this.textField;
            }
            set
            {
                this.textField = value;
            }
        }
    }

一切正常,直到到达文件的此部分:

Everything works fine until I reach this portion of the file:

<locations>
    <location country="PARAGUAY" city="Ciudad del Este" state="Alto Parana" xsi:nil="true"/>
    <location country="BRAZIL" city="Passo Fundo" state="Rio Grande do Sul" xsi:nil="true"/>
</locations>

在MSDN中定义,带有xsi:nil = true的元素将反序列化为null对象,完全失去所有属性。在C#中,这转换为空对象。

As stated in the MSDN, an element with xsi:nil="true" will be deserialized as a null object, losing all attributes completely. In C# this translates in a null object.

是否可以更改此行为,以使三个属性反序列化?

Is there a way to change this behaviour so to have the three properties deserialized?

谢谢您的任何建议!

编辑1:

这是关联的名称空间:

<records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="structure.xsd">
    (location is within here somewhere)
</records>


推荐答案

如果您只是想丢弃 xsi:nil 属性,设置 XmlElementAttribute.IsNullable = false 在属性中的 [XmlElement] 属性中包含引用位置的类。
例如如果您有

If you simply want to discard the xsi:nil attribute, set XmlElementAttribute.IsNullable = false in the [XmlElement] attribute on the property in the containing class that refers to location. E.g. if you have

[System.Xml.Serialization.XmlRootAttribute("locations", Namespace = "")]
public class LocationList
{
    [XmlElement("location", IsNullable = true)]
    public List<location> Locations { get; set; }
}

手动将其更改为:

[System.Xml.Serialization.XmlRootAttribute("locations", Namespace = "")]
public class LocationList
{
    [XmlElement("location", IsNullable = false)]
    public List<location> Locations { get; set; }
}

如果要绑定到该值 xsi:nil 的同时绑定到其他属性值,除了设置 IsNullable = false ,您还可以添加手动属性,与在XSD C#生成的类创建的XML中,我可以在同一个标​​记中具有null属性和其他属性吗?

If you want to bind to the value of xsi:nil while simultaneously binding to the other attribute values, in addition to setting IsNullable = false, you may add a manual property along the lines of the one shown in Can I have null attribute and other attribute at the same tag in XML created by XSD C# generated class?:

public partial class location
{
    bool nil;

    [XmlAttribute("nil", Namespace = "http://www.w3.org/2001/XMLSchema-instance")]
    public bool Nil
    {
        get
        {
            return nil;
        }
        set
        {
            nil = value;
        }
    }

    public bool ShouldSerializeNil() { return nil == true; }
}

样本小提琴

您还可以对XML进行预处理和后处理,以重命名 xsi: nil 属性,如此答案

You could also pre- and post-process the XML to rename the xsi:nil attribute, as was suggested in this answer.

这篇关于使用xsi:nil =“ true”反序列化XML元素。在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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