.NET XMLSerializer忽略序列化属性

时间:2018-11-13 16:31:00

标签: c# .net xsd xmlserializer primitive-types

我有一个由xsd.exe工具生成的类。不管是哪个类,总是会发生相同的问题,即在序列化对象时,将忽略字符串类型以外的每个属性。

对由serialize方法生成的xml进行反序列化将生成一个有效的.NET对象,该对象的所有属性(除字符串之外)的类型均为空(int,bool等)

我尝试修改xml,添加属性并再次反序列化。这似乎可行。

编辑: 我刚刚创建了一个演示模式,并使用xsd工具生成了一个C#类。 我的架构如下:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="http://PwC.C2C.Schemas.Internal.Schema1" xmlns:b="http://schemas.microsoft.com/BizTalk/2003" targetNamespace="http://PwC.C2C.Schemas.Internal.Schema1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="Root">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="TestElement1">
          <xs:complexType>
            <xs:attribute name="AttributeBool" type="xs:boolean" />
            <xs:attribute name="AttributeString" type="xs:string" />
            <xs:attribute name="AttributeInt" type="xs:int" />
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
    </xs:element>
</xs:schema>

生成的C#类如下:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,    Namespace="http://PwC.C2C.Schemas.Internal.Schema1")]    [System.Xml.Serialization.XmlRootAttribute(Namespace="http://PwC.C2C.Schemas.Internal.Schema1", IsNullable=false)]

public partial class Root {

    private RootTestElement1 testElement1Field;
    /// <remarks/>
  [System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
    public RootTestElement1 TestElement1 {
        get {
            return this.testElement1Field;
        }
        set {
            this.testElement1Field = value;
        }
    }
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true,  Namespace="http://PwC.C2C.Schemas.Internal.Schema1")]
public partial class RootTestElement1 {

    private bool attributeBoolField;

    private bool attributeBoolFieldSpecified;

    private string attributeStringField;

    private int attributeIntField;

    private bool attributeIntFieldSpecified;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool AttributeBool {
        get {
        return this.attributeBoolField;
        }
        set {
            this.attributeBoolField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool AttributeBoolSpecified {
        get {
            return this.attributeBoolFieldSpecified;
        }
        set {
            this.attributeBoolFieldSpecified = value;
        }
    }

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

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public int AttributeInt {
        get {
        return this.attributeIntField;
    }
        set {
            this.attributeIntField = value;
        }
    }

    /// <remarks/>
     [System.Xml.Serialization.XmlIgnoreAttribute()]
        public bool AttributeIntSpecified {
        get {
            return this.attributeIntFieldSpecified;
        }
        set {
            this.attributeIntFieldSpecified = value;
        }
    }
}

如果架构中指定的属性类型不是字符串,则有趣的是添加的额外属性。 attributeIntFieldSpecifiedattributeBoolFieldSpecified

这导致序列化类时忽略属性,需要将其完全删除或设置为true。

0 个答案:

没有答案