开发者

XML Deserialization Producing Incorrect Results

I am an XML beginner, using C# .NET 2.0/Visual Studio 2005.

What I have is a data sample in XML that I'd like to convert into strongly typed data structures. I also have an XSD for that file that I ran through the Visual Studio xsd.exe to generate the code for it. I ran it against System.Xml.Serialization.XmlSerializer It did a decent job but it needs some tweaking.

Running a test against it using this code:

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;
}

Where the test code simply prints out the account number from each record.

The problem is that the generated code is ignoring one half of the records in the file. It has to do with the <Assoc-Record> element, I think.

<?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>
    ...

The generated code from xsd.exe is this (a snippet):

[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 assocnoFi开发者_StackOverflow中文版eld;
    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;
        }
    }
}

It looks like xsd.exe is incorrectly assuming something about <Assoc-Record>, but nothing I've tried so far has helped. Trying to set <Assoc-Record> as an array attribute (as best as I could figure out) yielded no results; the code runs but <Assoc-Record> does not contain any <statement-record> elements (hows a zero-length array).

Also, xsd.exe at first turned AidStatements.assocRecordField into a 2D array, which I changed to a 1D array to get it to run in the current state.

Any ideas?


The AidStatements class should look like this:

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

The XSD schema looks quite confusing - instead of sequences containing elements of certain type the sequences contain references to a element and this probably confuses xsd.exe (all those XmlTypeAttribute(AnonymousType=true) look weird).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜