开发者

Serialization Error:Unable to generate a temporary class (result=1)

Running XSD.exe on my xml to generate C# class. All works well except on this property

 [System.Xml.Serialization.XmlArrayAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("ATTRIBUTES", typeof(DOCUMENTDocumentATTRIBUTES), Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
   public DocumentATTRIBUTES[][] Document {
    get {
        return this.documentField;
    }
    set {
        this.documentField = value;
    }
}

I want to try and use CollectionBase, and this was my attempt

     public DocumentATTRIBUTESCollection Document {
        get {
            return this.documentField;
        }
        set {
            this.documentField = value;
        }
    }


/// <remarks/>
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class DocumentATTRIBUTES
{

         private string _author;

        private string _maxVersions;

        private string _summary;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string author
        {
            get
            { return _author; }

            set { _author = value; }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string max_versions
        {
            get { return _maxVersions; }
            set { _maxVersions = value; }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public string summary
        {
            get { return _summary; }
            set { _summary = value; }
        }

}


public class DocumentAttributeCollection : System.Collections.CollectionBase
{
    public DocumentAttributeCollection() : base() { }

    public DocumentATTRIBUTES this[int index]
    {
        get
        {
            return (DocumentATTRIBU开发者_Go百科TES)this.InnerList[index];
        }
    }

    public void Insert(int index, DocumentATTRIBUTES value)
    {
        this.InnerList.Insert(index, value);
    }

    public int Add(DocumentATTRIBUTES value)
    {
        return (this.InnerList.Add(value));
    }
}

However when I try to serialize my object using

XmlSerializer serializer = new XmlSerializer(typeof(DocumentMetaData));

I get the error:

{"Unable to generate a temporary class (result=1).\r\nerror CS0030:
Cannot convert type 'DocumentATTRIBUTES' to 'DocumentAttributeCollection'\r\nerror CS1502: The best overloaded method match for 'DocumentAttributeCollection.Add(DocumentATTRIBUTES)' has some invalid arguments\r\nerror CS1503: Argument '1': cannot convert from 'DocumentAttributeCollection' to 'DocumentATTRIBUTES'\r\n"}

the XSD pertaining to this property is

<xs:complexType>
  <xs:sequence>
    <xs:element name="ATTRIBUTES" minOccurs="0" maxOccurs="unbounded">
      <xs:complexType>
        <xs:sequence>
          <xs:element name="author" type="xs:string" minOccurs="0" />
          <xs:element name="max_versions" type="xs:string" minOccurs="0" />
          <xs:element name="summary" type="xs:string" minOccurs="0" />
        </xs:sequence>
      </xs:complexType>
    </xs:element>
  </xs:sequence>
</xs:complexType>
</xs:element>

UPDATE: I changed to the following:

[System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
    [System.Xml.Serialization.XmlArrayItemAttribute("DocumentAttributes", typeof(DocumentAttributes), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
    public DocumentAttributeCollection Document
    {
        get { return _documentField; }
        set { _documentField = value; }
    }

I get past the pesky error , however my serialized object has no data pertaining to 'ATTRIBUTES' element.


Solved: I had this in my code

[System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("DocumentAttributes", typeof(DocumentAttributes), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
public DocumentAttributeCollection Document
{
    get { return _documentField; }
    set { _documentField = value; }
}

I made a mistake of having the attribute state 'DocumentAttributes" instead of 'ATTRIBUTES'

Changed the attribute to

[System.Xml.Serialization.XmlArrayAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlArrayItemAttribute("ATTRIBUTES", typeof(DocumentAttributes), Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
public DocumentAttributeCollection Document
{
    get { return _documentField; }
    set { _documentField = value; }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜