XML反序列化生成不正确的结果

时间:2010-01-06 22:12:17

标签: c# xml visual-studio-2005 xml-serialization xsd

我是一名XML初学者,使用的是C#.NET 2.0 / Visual Studio 2005.

我所拥有的是XML中的数据样本,我想将其转换为强类型数据结构。我还通过Visual Studio xsd.exe运行该文件的XSD,以便为其生成代码。我是针对System.Xml.Serialization.XmlSerializer进行的。它做得不错,但需要进行一些调整。

使用以下代码对其进行测试:

static int Main() {
    System.IO.StreamReader str = new System.IO.StreamReader(
        @"C:\xmlstm-2009122816413365.xml");
    System.Xml.Serialization.XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(typeof(AidStatements));
    AidStatements statementDataSet = (AidStatements)xSerializer.Deserialize(str);

    foreach (AssocRecord associations in statementDataSet.AssocRecords) {
        foreach (statementrecord statement in associations.statementrecord) {
            Console.WriteLine(statement.acctno);
        }
    }

    str.Close();

    Console.ReadLine();

    return 0;
}

测试代码只是打印出每条记录中的帐号。

问题是生成的代码忽略了文件中的一半记录。我认为它与<Assoc-Record>元素有关。

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="Aid-Statements">
        <xs:complexType>
            <xs:sequence>
                <xs:element maxOccurs="unbounded" ref="Assoc-Record"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="Assoc-Record">
    <xs:complexType>
        <xs:sequence>
            <xs:element maxOccurs="unbounded" ref="statement-record"/>
        </xs:sequence>
    </xs:complexType>
    </xs:element>
    <xs:element name="statement-record">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="assoc-no"/>
                <xs:element ref="acct-no"/>
                <xs:element ref="acct-no-mask"/>
                <xs:element ref="member-no"/>
                <xs:element ref="cr-code"/>
                <xs:element ref="stm-bal-fwd-date"/>
                <xs:element ref="stm-close-date"/>
                <xs:element ref="stm-due-date"/>
                <xs:element ref="prop-address-info"/>
                <xs:element ref="own-mail-info"/>
                <xs:element ref="messages"/>
                <xs:element ref="balances"/>
                <xs:element ref="trx-detail"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    ...

xsd.exe生成的代码就是这个(代码段):

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute("Aid-Statements", Namespace="", IsNullable=false)]
public partial class AidStatements {

    private statementrecord[] assocRecordField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayAttribute("Assoc-Record")]
    [System.Xml.Serialization.XmlArrayItemAttribute("statement-record", typeof(statementrecord), IsNullable=false)]
    public statementrecord[] AssocRecord {
        get {
            return this.assocRecordField;
        }
        set {
            this.assocRecordField = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute("statement-record", Namespace="", IsNullable=false)]
public partial class statementrecord {
    private string assocnoField;
    private string acctnoField;
    private string acctnomaskField;
    private string membernoField;
    private string crcodeField;
    private string stmbalfwddateField;
    private string stmclosedateField;
    private string stmduedateField;
    private propaddressinfo propaddressinfoField;
    private ownmailinfo ownmailinfoField;
    private messages messagesField;
    private balances balancesField;
    private trxrecord[] trxdetailField;
    // Matching properties below this
}

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute("Assoc-Record", Namespace="", IsNullable=false)]
public partial class AssocRecord {

    private statementrecord[] statementrecordField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("statement-record")]
    public statementrecord[] statementrecord {
        get {
            return this.statementrecordField;
        }
        set {
            this.statementrecordField = value;
        }
    }
}

看起来xsd.exe错误地假设有关<Assoc-Record>的内容,但到目前为止我没有尝试过任何帮助。试图将<Assoc-Record>设置为数组属性(尽我所能得出)并没有产生任何结果;代码运行但<Assoc-Record>不包含任何<statement-record>元素(如果是零长度数组)。

此外,xsd.exe首先将AidStatements.assocRecordField转换为2D数组,我将其更改为1D数组以使其在当前状态下运行。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

AidStatements类应如下所示:

public partial class AidStatements
{
  [XmlElement("Assoc-Record")]
  public AssocRecord[] AssocRecords;
}

XSD架构看起来很混乱 - 而不是包含特定类型元素的序列,序列包含对元素的引用,这可能会使xsd.exe混淆(所有XmlTypeAttribute(AnonymousType=true)看起来很奇怪)。