开发者

ASP.NET Xml doesn't deserialize to C# Class with any initialized variables correctly

开发者 https://www.devze.com 2023-02-05 14:43 出处:网络
I have an xml document I\'m trying to deserialize to a class that was generated from the xsd of the document,

I have an xml document I'm trying to deserialize to a class that was generated from the xsd of the document,

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

EDIT: Here's MORE of the generated C# classes, they've changed a bit since my initial question.

        [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()
        {
        }
    }

Here's the code I'm using to Deserialize the document,

        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;

All of this works, however the object that is returned after deserilization is empty and doesn't reflect any of the values found in the xml string that is being deserialized. Does anyone know what is wrong here? Thanks for any help!


I'm still not sure why this was happening, but it looks like I've resolve the issue for now by changing the schema namespace in my xsd to,

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

Which causes the C# classes to generate with an empty namespace whereas before they had the attribute,

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

Now they have,

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

The question I still have for anyone who can answer it is, why didn't the XmlSerializer give me an error before instead of returning a valueless instance of my object?

0

精彩评论

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