使用OAI-PMH协议不能多次序列化XML

时间:2016-04-17 06:13:19

标签: c# xml serialization oai

我希望使用XML协议获取OAI-PMH格式数据。当我使用XML反映f XmlSerializer时间resumption token时,没有问题,但当我尝试通过XML获取网址内容时,我在尝试反序列化时遇到以下错误{ {1}},尽管xmlreader正确读取了内容:

  

System.Xml.dll中出现未处理的“System.InvalidOperationException”类型异常

     

其他信息:XML文档中存在错误(149,26)。

     

InnerException:对于无符号字节,值太大或太小。

代码是这样的:

//getting content for the first time
string urlContent = BaseCommons.HttpHandler.getContent(first-time-url-link);
StreamReader OAIStream = new StreamReader(new MemoryStream(
                                          Encoding.UTF8.GetBytes(urlContent)));
var reader = XmlReader.Create(
             OAIStream.BaseStream, new XmlReaderSettings() { 
             ConformanceLevel = ConformanceLevel.Document });
OAI.OAIPMH OAIRes = new XmlSerializer(typeof(OAI.OAIPMH)).Deserialize(
                    reader) as OAI.OAIPMH;
//getting content for more than once
while (OAIRes.ListRecords.resumptionToken.Value != null && 
       OAIRes.ListRecords.resumptionToken.Value.ToString() != "")
      {
       string nextUrl = BaseCommons.HttpHandler.getContent(second-url-with-resumption + 
                        OAIRes.ListRecords.resumptionToken.Value.ToString());
       StreamReader OAIStream1 = new StreamReader(
                                new MemoryStream(Encoding.UTF8.GetBytes(nextUrl)));
       //xmlreader correctly reads content
       XmlReader reader1 = XmlReader.Create(OAIStream1.BaseStream, new XmlReaderSettings() { ConformanceLevel = ConformanceLevel.Document });
       // the error occured here 
       OAI.OAIPMH OAIRes1 = new XmlSerializer(typeof(OAI.OAIPMH)).Deserialize(reader1) as OAI.OAIPMH;
      }

根据the Microsoft Exception explanation,我向constructor添加了默认OAIPMH partial class,但发生了同样的错误。 OAI类是xml对象类,如下所示:

  public class OAI{

            /// <remarks/>
            [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
            [System.Xml.Serialization.XmlRootAttribute("OAI-PMH", Namespace="http://www.openarchives.org/OAI/2.0/", IsNullable = false)]
          public partial class OAIPMH
           {

            private System.DateTime responseDateField;

            private OAIPMHRequest requestField;

            private OAIPMHListRecords listRecordsField;

            /// <remarks/>
            public OAIPMH() { }
            public System.DateTime responseDate
            {
                get
                {
                    return this.responseDateField;
                }
                set
                {
                    this.responseDateField = value;
                }
            }

            /// <remarks/>
            public OAIPMHRequest request
            {
                get
                {
                    return this.requestField;
                }
                set
                {
                    this.requestField = value;
                }
            }

            /// <remarks/>
            public OAIPMHListRecords ListRecords
            {
                get
                {
                    return this.listRecordsField;
                }
                set
                {
                    this.listRecordsField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
        public partial class OAIPMHRequest
        {

            private string verbField;

            private string metadataPrefixField;

            private System.DateTime fromField;

            private System.DateTime untilField;

            private string valueField;

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string verb
            {
                get
                {
                    return this.verbField;
                }
                set
                {
                    this.verbField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string metadataPrefix
            {
                get
                {
                    return this.metadataPrefixField;
                }
                set
                {
                    this.metadataPrefixField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute(DataType = "date")]
            public System.DateTime from
            {
                get
                {
                    return this.fromField;
                }
                set
                {
                    this.fromField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute(DataType = "date")]
            public System.DateTime until
            {
                get
                {
                    return this.untilField;
                }
                set
                {
                    this.untilField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTextAttribute()]
            public string Value
            {
                get
                {
                    return this.valueField;
                }
                set
                {
                    this.valueField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
        public partial class OAIPMHListRecords
        {

            private OAIPMHListRecordsRecord[] recordField;

            private OAIPMHListRecordsResumptionToken resumptionTokenField;

            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("record")]
            public OAIPMHListRecordsRecord[] record
            {
                get
                {
                    return this.recordField;
                }
                set
                {
                    this.recordField = value;
                }
            }

            /// <remarks/>
            public OAIPMHListRecordsResumptionToken resumptionToken
            {
                get
                {
                    return this.resumptionTokenField;
                }
                set
                {
                    this.resumptionTokenField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
        public partial class OAIPMHListRecordsRecord
        {

            private OAIPMHListRecordsRecordHeader headerField;

            private OAIPMHListRecordsRecordMetadata metadataField;

            /// <remarks/>
            public OAIPMHListRecordsRecordHeader header
            {
                get
                {
                    return this.headerField;
                }
                set
                {
                    this.headerField = value;
                }
            }

            /// <remarks/>
            public OAIPMHListRecordsRecordMetadata metadata
            {
                get
                {
                    return this.metadataField;
                }
                set
                {
                    this.metadataField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
        public partial class OAIPMHListRecordsRecordHeader
        {

            private string identifierField;

            private System.DateTime datestampField;

            private string setSpecField;

            private string statusField;

            /// <remarks/>
            public string identifier
            {
                get
                {
                    return this.identifierField;
                }
                set
                {
                    this.identifierField = value;
                }
            }

            /// <remarks/>
            public System.DateTime datestamp
            {
                get
                {
                    return this.datestampField;
                }
                set
                {
                    this.datestampField = value;
                }
            }

            /// <remarks/>
            public string setSpec
            {
                get
                {
                    return this.setSpecField;
                }
                set
                {
                    this.setSpecField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string status
            {
                get
                {
                    return this.statusField;
                }
                set
                {
                    this.statusField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
        public partial class OAIPMHListRecordsRecordMetadata
        {

            private mods modsField;

            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.loc.gov/mods/v3")]
            public mods mods
            {
                get
                {
                    return this.modsField;
                }
                set
                {
                    this.modsField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
        [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.loc.gov/mods/v3", IsNullable = false)]
        public partial class mods
        {

            private modsTitleInfo titleInfoField;

            private modsName[] nameField;

            private string typeOfResourceField;

            private string genreField;

            private modsOriginInfo originInfoField;

            private modsLanguage languageField;

            private modsAbstract[] abstractField;

            private modsSubject subjectField;

            private modsRelatedItem relatedItemField;

            /// <remarks/>
            public modsTitleInfo titleInfo
            {
                get
                {
                    return this.titleInfoField;
                }
                set
                {
                    this.titleInfoField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("name")]
            public modsName[] name
            {
                get
                {
                    return this.nameField;
                }
                set
                {
                    this.nameField = value;
                }
            }

            /// <remarks/>
            public string typeOfResource
            {
                get
                {
                    return this.typeOfResourceField;
                }
                set
                {
                    this.typeOfResourceField = value;
                }
            }

            /// <remarks/>
            public string genre
            {
                get
                {
                    return this.genreField;
                }
                set
                {
                    this.genreField = value;
                }
            }

            /// <remarks/>
            public modsOriginInfo originInfo
            {
                get
                {
                    return this.originInfoField;
                }
                set
                {
                    this.originInfoField = value;
                }
            }

            /// <remarks/>
            public modsLanguage language
            {
                get
                {
                    return this.languageField;
                }
                set
                {
                    this.languageField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("abstract")]
            public modsAbstract[] @abstract
            {
                get
                {
                    return this.abstractField;
                }
                set
                {
                    this.abstractField = value;
                }
            }

            /// <remarks/>
            public modsSubject subject
            {
                get
                {
                    return this.subjectField;
                }
                set
                {
                    this.subjectField = value;
                }
            }

            /// <remarks/>
            public modsRelatedItem relatedItem
            {
                get
                {
                    return this.relatedItemField;
                }
                set
                {
                    this.relatedItemField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
        public partial class modsTitleInfo
        {

            private string titleField;

            /// <remarks/>
            public string title
            {
                get
                {
                    return this.titleField;
                }
                set
                {
                    this.titleField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
        public partial class modsName
        {

            private string namePartField;

            private string typeField;

            /// <remarks/>
            public string namePart
            {
                get
                {
                    return this.namePartField;
                }
                set
                {
                    this.namePartField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string type
            {
                get
                {
                    return this.typeField;
                }
                set
                {
                    this.typeField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
        public partial class modsOriginInfo
        {

            private string dateIssuedField;

            /// <remarks/>
            public string dateIssued
            {
                get
                {
                    return this.dateIssuedField;
                }
                set
                {
                    this.dateIssuedField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
        public partial class modsLanguage
        {

            private string languageTermField;

            /// <remarks/>
            public string languageTerm
            {
                get
                {
                    return this.languageTermField;
                }
                set
                {
                    this.languageTermField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
        public partial class modsAbstract
        {

            private string langField;

            private string typeField;

            private string valueField;

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://www.w3.org/XML/1998/namespace")]
            public string lang
            {
                get
                {
                    return this.langField;
                }
                set
                {
                    this.langField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string type
            {
                get
                {
                    return this.typeField;
                }
                set
                {
                    this.typeField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTextAttribute()]
            public string Value
            {
                get
                {
                    return this.valueField;
                }
                set
                {
                    this.valueField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
        public partial class modsSubject
        {

            private string topicField;

            private string geographicField;

            /// <remarks/>
            public string topic
            {
                get
                {
                    return this.topicField;
                }
                set
                {
                    this.topicField = value;
                }
            }

            /// <remarks/>
            public string geographic
            {
                get
                {
                    return this.geographicField;
                }
                set
                {
                    this.geographicField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
        public partial class modsRelatedItem
        {

            private modsRelatedItemTitleInfo titleInfoField;

            private modsRelatedItemPart partField;

            private string typeField;

            /// <remarks/>
            public modsRelatedItemTitleInfo titleInfo
            {
                get
                {
                    return this.titleInfoField;
                }
                set
                {
                    this.titleInfoField = value;
                }
            }

            /// <remarks/>
            public modsRelatedItemPart part
            {
                get
                {
                    return this.partField;
                }
                set
                {
                    this.partField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string type
            {
                get
                {
                    return this.typeField;
                }
                set
                {
                    this.typeField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
        public partial class modsRelatedItemTitleInfo
        {

            private string titleField;

            /// <remarks/>
            public string title
            {
                get
                {
                    return this.titleField;
                }
                set
                {
                    this.titleField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
        public partial class modsRelatedItemPart
        {

            private modsRelatedItemPartDetail[] detailField;

            private string dateField;

            /// <remarks/>
            [System.Xml.Serialization.XmlElementAttribute("detail")]
            public modsRelatedItemPartDetail[] detail
            {
                get
                {
                    return this.detailField;
                }
                set
                {
                    this.detailField = value;
                }
            }

            /// <remarks/>
            public string date
            {
                get
                {
                    return this.dateField;
                }
                set
                {
                    this.dateField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.loc.gov/mods/v3")]
        public partial class modsRelatedItemPartDetail
        {

            private byte numberField;

            private string captionField;

            private string typeField;

            /// <remarks/>
            public byte number
            {
                get
                {
                    return this.numberField;
                }
                set
                {
                    this.numberField = value;
                }
            }

            /// <remarks/>
            public string caption
            {
                get
                {
                    return this.captionField;
                }
                set
                {
                    this.captionField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public string type
            {
                get
                {
                    return this.typeField;
                }
                set
                {
                    this.typeField = value;
                }
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.openarchives.org/OAI/2.0/")]
        public partial class OAIPMHListRecordsResumptionToken
        {

            private System.DateTime expirationDateField;

            private ushort completeListSizeField;

            private byte cursorField;

            private string valueField;

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public System.DateTime expirationDate
            {
                get
                {
                    return this.expirationDateField;
                }
                set
                {
                    this.expirationDateField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public ushort completeListSize
            {
                get
                {
                    return this.completeListSizeField;
                }
                set
                {
                    this.completeListSizeField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlAttributeAttribute()]
            public byte cursor
            {
                get
                {
                    return this.cursorField;
                }
                set
                {
                    this.cursorField = value;
                }
            }

            /// <remarks/>
            [System.Xml.Serialization.XmlTextAttribute()]
            public string Value
            {
                get
                {
                    return this.valueField;
                }
                set
                {
                    this.valueField = value;
                }
            }
        }

        }

如何多次反序列化xml

0 个答案:

没有答案