将多个选择元素从XML序列化为C#

时间:2019-04-03 08:34:19

标签: c# xml serialization

我有这个模式

<xs:complexType name="FatherElement">
    <xs:sequence>
        <xs:element ref="FatherClass"/>
        <xs:choice>
            <xs:sequence>
                <xs:element ref="FatherType"/>
                <xs:element ref="FatherLocation" minOccurs="0"/>
                <xs:element ref="FatherTypeDescription" minOccurs="0"/>
            </xs:sequence>
            <xs:sequence>
                <xs:element ref="FatherLocation"/>
                <xs:element ref="FatherTypeDescription" minOccurs="0"/>
            </xs:sequence>
            <xs:element ref="FatherTypeDescription"/>
        </xs:choice>
        <xs:element ref="FatherBasis"/>
        <xs:element ref="FatherRole" minOccurs="0"/>
        <xs:element name="Extension" type="FatherElement_ExtensionType" minOccurs="0"/>
    </xs:sequence>
</xs:complexType>

我正在尝试使用此C#映射进行映射(拥有所有字段会很好,但是现在不需要所有字段了

[System.Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[System.Xml.Serialization.XmlType(AnonymousType = true, Namespace = "http://www.lol.com/Standards/lol/1")]
public class FatherElement
{
    /// <remarks/>
    public string FatherTypeDescription { get; set; }

    /// <remarks/>
    public string FatherType { get; set; }

    /// <remarks/>
    public FatherLocation FatherLocation { get; set; }
}


[System.Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[System.Xml.Serialization.XmlType(AnonymousType = true, Namespace = "http://www.lol.com/Standards/lol/1")]
public class FatherLocation
{
    /// <remarks/>
    public FatherLocationLocation Location { get; set; }
}


[System.Serializable()]
[System.ComponentModel.DesignerCategory("code")]
[System.Xml.Serialization.XmlType(AnonymousType = true, Namespace = "http://www.lol.com/Standards/lol/1")]
public class FatherLocationLocation
{
    /// <remarks/>
    public string Country { get; set; }
}

我得到的传入XML值是

<FatherElement>
    <FatherClass>classValue</FatherClass> 
    <FatherType>typeValue</FatherType> 
    <FatherTypeDescription>typeValueDesc</FatherTypeDescription> 
    <FatherBasis>basisValue</FatherBasis> 
    <FatherRole>RoleValue</FatherRole> 
</FatherElement>

我出来的是:

<FatherElement>
    <FatherTypeDescription>typeValueDesc</FatherTypeDescription>
    <FatherType>typeValue</FatherType>
  </FatherElement>

当我尝试根据SDC对其进行验证时,我收到一条错误消息,指出元素FatherElement的子父亲类型无效。

我试图从XSD生成C#映射,但是它生成的代码将选择内容转换为类型对象的元素,并且我想保持强类型。

有什么主意吗?

2 个答案:

答案 0 :(得分:1)

<xs:choice>在生成类时仍然是一个问题。问题是所选元素的装箱和命名。我建议一种解决方法。

据我了解,您想在三种可能性之间进行选择:

  1. 父亲有Type,可以有LocationDescription

  2. 父亲有Location并且可以Description

  3. 父亲只有Description

问题在于,您选择中定义的序列类型将无法被完全识别(就像MaPi注释的那样,您必须使用ItemName-Enum和Item)。 您可以将可组合序列移动到元素中,以说明VS将其作为单个对象进行处理。这是一个示例(我确实用字符串替换了您的复杂类型,以实现可编译/可生成的示例):

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="FatherElement">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="FatherClass" type="xs:string"/>
                <xs:choice> <!-- In this choice we can choose 3 different elements -->
                    <xs:element name="CompleteFather"> <!-- 1 -->
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="FatherType" type="xs:string"/>
                                <xs:element name="FatherLocation" type="xs:string" minOccurs="0"/>
                                <xs:element name="FatherTypeDescription" type="xs:string" minOccurs="0"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="UncompletaFather"> <!-- 2 -->
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="FatherLocation" type="xs:string"/>
                                <xs:element name="FatherTypeDescription" type="xs:string" minOccurs="0"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                    <xs:element name="FatherTypeDescription" type="xs:string"/> <!-- 3 -->
                </xs:choice>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

这里有一些c#代码示例。

FatherElement e = new FatherElement();
e.FatherClass = "Some Element";

// Here we choose our element in our choice. It'll be boxed into an object.
e.Item = new FatherElementCompleteFather()
{
    FatherLocation = "loc",
    FatherType = "type",
    FatherTypeDescription = "desc"
};

string filePath = @"C:\Temp\test.xml";
XmlSerializer x = new XmlSerializer(e.GetType());

using (var sw = new StreamWriter(filePath))
    x.Serialize(sw, e);

答案 1 :(得分:1)

最后,我通过从xsd中提取映射来解决它。 看起来像这样:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.lol.com/Standards/lol/1")]
[System.Xml.Serialization.XmlRootAttribute("FatherProvision", Namespace = "http://www.lol.com/Standards/lol/1", IsNullable = false)]
public partial class FatherElement
{

    private string FatherClassField;

    private object[] itemsField;

    private ItemsChoiceType3[] itemsElementNameField;

    private string FatherBasisField;

    private string FatherRoleField;

    private FatherProvision_ExtensionType extensionField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "NMTOKEN")]
    public string FatherClass
    {
        get
        {
            return this.FatherClassField;
        }
        set
        {
            this.FatherClassField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("FatherAuthorityLocation", typeof(FatherAuthorityLocationType))]
    [System.Xml.Serialization.XmlElementAttribute("FatherType", typeof(string), DataType = "NMTOKEN")]
    [System.Xml.Serialization.XmlElementAttribute("FatherTypeDescription", typeof(string))]
    [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
    public object[] Items
    {
        get
        {
            return this.itemsField;
        }
        set
        {
            this.itemsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public ItemsChoiceType3[] ItemsElementName
    {
        get
        {
            return this.itemsElementNameField;
        }
        set
        {
            this.itemsElementNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "NMTOKEN")]
    public string FatherBasis
    {
        get
        {
            return this.FatherBasisField;
        }
        set
        {
            this.FatherBasisField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute(DataType = "NMTOKEN")]
    public string FatherRole
    {
        get
        {
            return this.FatherRoleField;
        }
        set
        {
            this.FatherRoleField = value;
        }
    }

    /// <remarks/>
    public FatherProvision_ExtensionType Extension
    {
        get
        {
            return this.extensionField;
        }
        set
        {
            this.extensionField = value;
        }
    }
}


public enum ItemsChoiceType3
{

    /// <remarks/>
    FatherAuthorityLocation,

    /// <remarks/>
    FatherType,

    /// <remarks/>
    FatherTypeDescription,
}

然后我首先通过检查名称元素数组中所需类型(如果有)的元素的索引来访问元素,然后使用该索引来访问元素。

var fatherTypeElement = string.Empty;
var fatherAuthorityLocationElement = (fatherAuthorityLocationType)null;
var fatherTypeElementIndex = Array.IndexOf(fatherProvisionAndPercentage.fatherProvision.ItemsElementName, ItemsChoiceType3.fatherType);
if(fatherTypeElementIndex >= 0)
    fatherTypeElement = fatherProvisionAndPercentage.fatherProvision.Items[fatherTypeElementIndex] as string;