2010-01-06 85 views
1

我是一個XML的初學者,使用C#.NET 2.0/Visual Studio中。我也有一個用於該文件的XSD,我通過Visual Studio xsd.exe運行以生成它的代碼。我跑了它對System.Xml.Serialization.XmlSerializer它做了一個體面的工作,但它需要一些調整。XML反序列化生產不正確的結果

使用此代碼運行反對測試:

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>元素(hows一個零長度數組)。

此外,xsd.exe首先將AidStatements.assocRecordField轉換爲二維數組,然後將其更改爲一維數組以使其在當前狀態下運行。

任何想法?

回答

1

AidStatements類應該是這樣的:

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

XSD架構看起來相當混亂 - 而不是包含某種類型的序列包含一個元素引用的元素序列,這可能混淆xsd.exe(所有這些XmlTypeAttribute(AnonymousType=true)看起來很奇怪)。

+0

哈哈,不是開玩笑。只要我看到這個模式,即使我不是XML/XSD的專家,我也很失望。 – Chris 2010-01-07 01:58:00

+0

我在第二天嘗試了這個修復程序,雖然之後沒有完美運行,但它確實讓我足夠接近,使我需要的更改能夠運行。謝謝! – Chris 2010-01-07 17:13:52