2011-01-20 18 views
0

我有一個XML文檔我試圖反序列化到從文檔的XSD生成的類,ASP.NET XML不反序列化到C#類與任何初始化變量正確

<DataRequest> 
    <Table Name="B_MH_SS_Buying_Influences" Destination="Influences"> 
    <Row> 
     <Field Name="ContactID" Destination="ContactID" Type="STANDARD"/> 
     <Field Name="RoleID" Destination="RoleID" Type="STANDARD"/> 
     <Field Name="RoleName" Destination="RoleName" Type="STANDARD"/> 
    </Row> 
    </Table> 
    <Field Name="AccountName" Destination="Account" Type="STANDARD"/> 
    <Field Name="OpportunityName" Destination="Product" Type="STANDARD"/> 
    <Field Name="OwnerName" Destination="SalesPerson" Type="STANDARD"/> 
    <Table Name="Contact" Type="STANDARD"> 
    <Row> 
     <Field Name="ID" Destination="ID" Type="STANDARD"/> 
     <Field Name="FirstName" Destination="FirstName" Type="STANDARD"/> 
     <Field Name="LastName" Destination="LastName" Type="STANDARD"/> 
     <Field Name="Title" Destination="Title" Type="string" Custom="True"/> 
    </Row> 
    </Table> 
    <Table Name="User" Type="STANDARD"> 
    <Row> 
     <Field Name="ID" Destination="ID" Type="STANDARD"/> 
     <Field Name="FirstName" Destination="FirstName" Type="STANDARD"/> 
     <Field Name="LastName" Destination="LastName" Type="STANDARD"/> 
    </Row> 
    </Table> 
    <Table Name="UserData" Type="STANDARD"> 
    <Row> 
     <Field Name="ReadOnly" Destination="ReadOnly" Type="STANDARD">False</Field> 
     <Field Name="DateFormat" Destination="DateFormat" Type="STANDARD">mdy</Field> 
     <Field Name="DateSeparator" Destination="DateSeparator" Type="STANDARD">/</Field> 
     <Field Name="Language" Destination="Language" Type="STANDARD"/> 
     <Field Name="CurrencySymbol" Destination="CurrencySymbol" Type="STANDARD"/> 
     <Field Name="Decimals" Destination="Decimals" Type="STANDARD">.</Field> 
     <Field Name="Thousands" Destination="Thousands" Type="STANDARD">,</Field> 
    </Row> 
    </Table> 
</DataRequest> 

編輯:這是更多生成的C#類,他們已經改變了一點,因爲我最初的問題。

 [XmlRoot(ElementName="DataRequest",Namespace=Declarations.SchemaVersion,IsNullable=false),Serializable] 
     public partial class DataRequest 
     { 

      [XmlElement(Type=typeof(Table),ElementName="Table",IsNullable=false,Form=XmlSchemaForm.Qualified,Namespace=Declarations.SchemaVersion)] 
      [EditorBrowsable(EditorBrowsableState.Advanced)] 
      public TableCollection __TableCollection; 

      [XmlIgnore] 
      public TableCollection TableCollection 
      { 
       get 
       { 
        if (__TableCollection == null) __TableCollection = new TableCollection(); 
        return __TableCollection; 
       } 
       set {__TableCollection = value;} 
      } 

      [XmlElement(Type=typeof(DataRequestField),ElementName="Field",IsNullable=false,Form=XmlSchemaForm.Qualified,Namespace=Declarations.SchemaVersion)] 
      [EditorBrowsable(EditorBrowsableState.Advanced)] 
      public DataRequestFieldCollection __FieldCollection; 

      [XmlIgnore] 
      public DataRequestFieldCollection FieldCollection 
      { 
       get 
       { 
        if (__FieldCollection == null) __FieldCollection = new DataRequestFieldCollection(); 
        return __FieldCollection; 
       } 
       set {__FieldCollection = value;} 
      } 

      [XmlElement(Type=typeof(SamPivotalConnectorWS.Models.Options),ElementName="Options",IsNullable=false,Form=XmlSchemaForm.Qualified,Namespace=Declarations.SchemaVersion)] 
      [EditorBrowsable(EditorBrowsableState.Advanced)] 
      public OptionsCollection __OptionsCollection; 

      [XmlIgnore] 
      public OptionsCollection OptionsCollection 
      { 
       get 
       { 
        if (__OptionsCollection == null) __OptionsCollection = new OptionsCollection(); 
        return __OptionsCollection; 
       } 
       set {__OptionsCollection = value;} 
      } 

      public DataRequest() 
      { 
      } 
     } 

    [XmlType(TypeName="Table",Namespace=Declarations.SchemaVersion),Serializable] 
     public partial class Table 
     { 
      [System.Runtime.InteropServices.DispIdAttribute(-4)] 
      public IEnumerator GetEnumerator() 
      { 
       return RowCollection.GetEnumerator(); 
      } 

      public TableRow Add(TableRow obj) 
      { 
       return RowCollection.Add(obj); 
      } 

      [XmlIgnore] 
      public TableRow this[int index] 
      { 
       get { return (TableRow) RowCollection[index]; } 
      } 

      [XmlIgnore] 
      public int Count 
      { 
       get { return RowCollection.Count; } 
      } 

      public void Clear() 
      { 
       RowCollection.Clear(); 
      } 

      public TableRow Remove(int index) 
      { 
       TableRow obj = RowCollection[index]; 
       RowCollection.Remove(obj); 
       return obj; 
      } 

      public void Remove(object obj) 
      { 
       RowCollection.Remove(obj); 
      } 

      [XmlAttribute(AttributeName="Name",DataType="string")] 
      [EditorBrowsable(EditorBrowsableState.Advanced)] 
      public string __Name; 

      [XmlIgnore] 
      public string Name 
      { 
       get { return __Name; } 
       set { __Name = value; } 
      } 

      [XmlAttribute(AttributeName="Destination",DataType="string")] 
      [EditorBrowsable(EditorBrowsableState.Advanced)] 
      public string __Destination; 

      [XmlIgnore] 
      public string Destination 
      { 
       get { return __Destination; } 
       set { __Destination = value; } 
      } 

      [XmlAttribute(AttributeName="Type",DataType="string")] 
      [EditorBrowsable(EditorBrowsableState.Advanced)] 
      public string __Type; 

      [XmlIgnore] 
      public string Type 
      { 
       get { return __Type; } 
       set { __Type = value; } 
      } 

      [XmlElement(Type=typeof(TableRow),ElementName="Row",IsNullable=false,Form=XmlSchemaForm.Qualified,Namespace=Declarations.SchemaVersion)] 
      [EditorBrowsable(EditorBrowsableState.Advanced)] 
      public TableRowCollection __RowCollection; 

      [XmlIgnore] 
      public TableRowCollection RowCollection 
      { 
       get 
       { 
        if (__RowCollection == null) __RowCollection = new TableRowCollection(); 
        return __RowCollection; 
       } 
       set {__RowCollection = value;} 
      } 

      public Table() 
      { 
      } 
     } 

    [XmlType(TypeName="TableRow",Namespace=Declarations.SchemaVersion),Serializable] 
     public partial class TableRow 
     { 
      [System.Runtime.InteropServices.DispIdAttribute(-4)] 
      public IEnumerator GetEnumerator() 
      { 
       return FieldCollection.GetEnumerator(); 
      } 

      public TableRowField Add(TableRowField obj) 
      { 
       return FieldCollection.Add(obj); 
      } 

      [XmlIgnore] 
      public TableRowField this[int index] 
      { 
       get { return (TableRowField) FieldCollection[index]; } 
      } 

      [XmlIgnore] 
      public int Count 
      { 
       get { return FieldCollection.Count; } 
      } 

      public void Clear() 
      { 
       FieldCollection.Clear(); 
      } 

      public TableRowField Remove(int index) 
      { 
       TableRowField obj = FieldCollection[index]; 
       FieldCollection.Remove(obj); 
       return obj; 
      } 

      public void Remove(object obj) 
      { 
       FieldCollection.Remove(obj); 
      } 

      [XmlElement(Type=typeof(TableRowField),ElementName="Field",IsNullable=false,Form=XmlSchemaForm.Qualified,Namespace=Declarations.SchemaVersion)] 
      [EditorBrowsable(EditorBrowsableState.Advanced)] 
      public TableRowFieldCollection __FieldCollection; 

      [XmlIgnore] 
      public TableRowFieldCollection FieldCollection 
      { 
       get 
       { 
        if (__FieldCollection == null) __FieldCollection = new TableRowFieldCollection(); 
        return __FieldCollection; 
       } 
       set {__FieldCollection = value;} 
      } 

      public TableRow() 
      { 
      } 
     } 
[XmlType(TypeName="TableRowField",Namespace=Declarations.SchemaVersion),Serializable] 
    public partial class TableRowField 
    { 

     [XmlAttribute(AttributeName="Name",DataType="string")] 
     [EditorBrowsable(EditorBrowsableState.Advanced)] 
     public string __Name; 

     [XmlIgnore] 
     public string Name 
     { 
      get { return __Name; } 
      set { __Name = value; } 
     } 

     [XmlAttribute(AttributeName="Destination",DataType="string")] 
     [EditorBrowsable(EditorBrowsableState.Advanced)] 
     public string __Destination; 

     [XmlIgnore] 
     public string Destination 
     { 
      get { return __Destination; } 
      set { __Destination = value; } 
     } 

     [XmlAttribute(AttributeName="Type",DataType="string")] 
     [EditorBrowsable(EditorBrowsableState.Advanced)] 
     public string __Type; 

     [XmlIgnore] 
     public string Type 
     { 
      get { return __Type; } 
      set { __Type = value; } 
     } 

     [XmlText(DataType="string")] 
     [EditorBrowsable(EditorBrowsableState.Advanced)] 
     public string __Value; 

     [XmlIgnore] 
     public string Value 
     { 
      get { return __Value; } 
      set { __Value = value; } 
     } 

     public TableRowField() 
     { 
     } 
    } 

下面是我使用的反序列化文檔的代碼,

 XmlRootAttribute xRoot = new XmlRootAttribute(); 
     xRoot.ElementName = "DataRequest"; 
     xRoot.IsNullable = true; 
     XmlSerializer serializer = new XmlSerializer(typeof(DataRequest), xRoot); 
     req = serializer.Deserialize(Context.Request.InputStream) as DataRequest; 

所有這一切工作,但之後deserilization是空的,並不反映任何值時,返回的對象在被反序列化的xml字符串中找到。有人知道這裏有什麼問題嗎?謝謝你的幫助!

+0

所有其他*元素的名稱空間是什麼? – 2011-01-20 19:14:49

回答

0

我仍然不知道爲什麼這是怎麼回事,但它看起來像我已經解決這個問題,現在在我的XSD更改架構命名空間,

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 

這使得C#類一個空的命名空間,但是他們的屬性之前產生,

[XmlRoot(ElementName="DataRequest",Namespace=Declarations.SchemaVersion,IsNullable=false),Serializable] 

現在,他們有,

[XmlRoot(ElementName="DataRequest",IsNullable=false),Serializable] 

對於任何可以回答的問題,我仍然有一個問題,那就是爲什麼XmlSerializer之前沒有給我一個錯誤,而不是返回我的對象​​的無價值實例?