Note that there are some explanatory texts on larger screens.

plurals
  1. POXML Deserialization Producing Incorrect Results
    primarykey
    data
    text
    <p>I am an XML beginner, using C# .NET 2.0/Visual Studio 2005. </p> <p>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 <code>xsd.exe</code> to generate the code for it. I ran it against <code>System.Xml.Serialization.XmlSerializer</code> It did a decent job but it needs some tweaking.</p> <p>Running a test against it using this code:</p> <pre><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; } </code></pre> <p>Where the test code simply prints out the account number from each record.</p> <p>The problem is that the generated code is ignoring one half of the records in the file. It has to do with the <code>&lt;Assoc-Record&gt;</code> element, I think.</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified"&gt; &lt;xs:element name="Aid-Statements"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element maxOccurs="unbounded" ref="Assoc-Record"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="Assoc-Record"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element maxOccurs="unbounded" ref="statement-record"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; &lt;xs:element name="statement-record"&gt; &lt;xs:complexType&gt; &lt;xs:sequence&gt; &lt;xs:element ref="assoc-no"/&gt; &lt;xs:element ref="acct-no"/&gt; &lt;xs:element ref="acct-no-mask"/&gt; &lt;xs:element ref="member-no"/&gt; &lt;xs:element ref="cr-code"/&gt; &lt;xs:element ref="stm-bal-fwd-date"/&gt; &lt;xs:element ref="stm-close-date"/&gt; &lt;xs:element ref="stm-due-date"/&gt; &lt;xs:element ref="prop-address-info"/&gt; &lt;xs:element ref="own-mail-info"/&gt; &lt;xs:element ref="messages"/&gt; &lt;xs:element ref="balances"/&gt; &lt;xs:element ref="trx-detail"/&gt; &lt;/xs:sequence&gt; &lt;/xs:complexType&gt; &lt;/xs:element&gt; ... </code></pre> <p>The generated code from <code>xsd.exe</code> is this (a snippet):</p> <pre><code>[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; /// &lt;remarks/&gt; [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 } /// &lt;remarks/&gt; [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; /// &lt;remarks/&gt; [System.Xml.Serialization.XmlElementAttribute("statement-record")] public statementrecord[] statementrecord { get { return this.statementrecordField; } set { this.statementrecordField = value; } } } </code></pre> <p>It looks like <code>xsd.exe</code> is incorrectly assuming something about <code>&lt;Assoc-Record&gt;</code>, but nothing I've tried so far has helped. Trying to set <code>&lt;Assoc-Record&gt;</code> as an array attribute (as best as I could figure out) yielded no results; the code runs but <code>&lt;Assoc-Record&gt;</code> does not contain any <code>&lt;statement-record&gt;</code> elements (hows a zero-length array).</p> <p>Also, <code>xsd.exe</code> at first turned <code>AidStatements.assocRecordField</code> into a 2D array, which I changed to a 1D array to get it to run in the current state.</p> <p>Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload