反序列化 XML 并在 XML 文档中出现错误 (2, 2)

Deserializing XML and Getting an Error in XML Document (2, 2)

我有一个 XML 文件正在读取并试图反序列化为一个对象。我在尝试时收到此错误:

System.InvalidOperationException: 'There is an error in XML document (2, 2).' InvalidOperationException: 不是预期的。

这是 XML 文件:

<?xml version="1.0" encoding="utf-8" ?>
<ns6:ACABulkRequestTransmitterStatusDetailResponse xmlns="urn:us:gov:treasury:irs:ext:aca:air:ty20" xmlns:ns2="urn:us:gov:treasury:irs:common" xmlns:ns3="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:ns4="http://www.w3.org/2000/09/xmldsig#" xmlns:ns5="urn:us:gov:treasury:irs:msg:acasecurityheader" xmlns:ns6="urn:us:gov:treasury:irs:msg:irstransmitterstatusrequest">
    <ACABulkRequestTransmitterResponse>
        <TransmissionStatusCd>Rejected</TransmissionStatusCd>
        <ns2:ReceiptId>AATS1094B-21-00008699</ns2:ReceiptId>
    </ACABulkRequestTransmitterResponse>
    <ACABulkReqTrnsmtStsRespGrpDtl>
        <ns2:BulkExchangeFile>
            <xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:46834192-2caa-4016-ab11-8a48e0cbeb6c-194@urn%3Aus%3Agov%3Atreasury%3Airs%3Acommon"/>
        </ns2:BulkExchangeFile>
    </ACABulkReqTrnsmtStsRespGrpDtl>
</ns6:ACABulkRequestTransmitterStatusDetailResponse>

这是我使用的所有 C# 代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (TextReader reader = File.OpenText(@"C:\Projects\Prototypes\IrsAcaClient\ConsoleApp1\XMLFile1.xml"))
            {
                string xml = reader.ReadToEnd();
                var o = Deserialize<ACABulkRequestTransmitterStatusDetailResponse>(xml);
            }
        }

        /// <summary>
        /// deserializes the xml string into an object
        /// </summary>
        /// <param name="xmlString"></param>
        /// <returns></returns>
        public static T Deserialize<T>(string xmlString) where T : class
        {
            //if the string is empty, just return null
            if (xmlString.Length <= 0)
            {
                return null;
            }

            //create a serializer
            var serializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
            T output;
            //create the reader that the serializer will read from, passing it the string
            using (var reader = new System.IO.StringReader(xmlString))
            {
                //rebuild the list object
                output = (T)serializer.Deserialize(reader);
            }
            //return the list
            return output;
        }
    }

    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:us:gov:treasury:irs:msg:irstransmitterstatusrequest")]
    public partial class ACABulkRequestTransmitterStatusDetailResponse : object, System.ComponentModel.INotifyPropertyChanged
    {

        private ACABulkRequestTransmitterResponseType aCABulkRequestTransmitterResponseField;

        private ACABulkReqTrnsmtStsRespGrpDtlType aCABulkReqTrnsmtStsRespGrpDtlField;

        private string versionField;

        public ACABulkRequestTransmitterStatusDetailResponse()
        {
            this.versionField = "1.0";
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20", Order = 0)]
        public ACABulkRequestTransmitterResponseType ACABulkRequestTransmitterResponse
        {
            get
            {
                return this.aCABulkRequestTransmitterResponseField;
            }
            set
            {
                this.aCABulkRequestTransmitterResponseField = value;
                this.RaisePropertyChanged("ACABulkRequestTransmitterResponse");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20", Order = 1)]
        public ACABulkReqTrnsmtStsRespGrpDtlType ACABulkReqTrnsmtStsRespGrpDtl
        {
            get
            {
                return this.aCABulkReqTrnsmtStsRespGrpDtlField;
            }
            set
            {
                this.aCABulkReqTrnsmtStsRespGrpDtlField = value;
                this.RaisePropertyChanged("ACABulkReqTrnsmtStsRespGrpDtl");
            }
        }

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

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20")]
    public enum TransmissionStatusCodeType
    {

        /// <remarks/>
        Accepted,

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("Accepted with Errors")]
        AcceptedwithErrors,

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("Partially Accepted")]
        PartiallyAccepted,

        /// <remarks/>
        Rejected,

        /// <remarks/>
        Processing,

        /// <remarks/>
        [System.Xml.Serialization.XmlEnumAttribute("Not Found")]
        NotFound,
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:us:gov:treasury:irs:common")]
    public partial class ErrorMessageDetailType : object, System.ComponentModel.INotifyPropertyChanged
    {

        private string errorMessageCdField;

        private string errorMessageTxtField;

        private string xpathContentField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
        public string ErrorMessageCd
        {
            get
            {
                return this.errorMessageCdField;
            }
            set
            {
                this.errorMessageCdField = value;
                this.RaisePropertyChanged("ErrorMessageCd");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 1)]
        public string ErrorMessageTxt
        {
            get
            {
                return this.errorMessageTxtField;
            }
            set
            {
                this.errorMessageTxtField = value;
                this.RaisePropertyChanged("ErrorMessageTxt");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 2)]
        public string XpathContent
        {
            get
            {
                return this.xpathContentField;
            }
            set
            {
                this.xpathContentField = value;
                this.RaisePropertyChanged("XpathContent");
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20")]
    public partial class ACABulkRequestTransmitterResponseType : object, System.ComponentModel.INotifyPropertyChanged
    {

        private TransmissionStatusCodeType transmissionStatusCdField;

        private string receiptIdField;

        private ErrorMessageDetailType errorMessageDetailField;

        private string versionField;

        public ACABulkRequestTransmitterResponseType()
        {
            this.versionField = "1.0";
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Order = 0)]
        public TransmissionStatusCodeType TransmissionStatusCd
        {
            get
            {
                return this.transmissionStatusCdField;
            }
            set
            {
                this.transmissionStatusCdField = value;
                this.RaisePropertyChanged("TransmissionStatusCd");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:common", Order = 1)]
        public string ReceiptId
        {
            get
            {
                return this.receiptIdField;
            }
            set
            {
                this.receiptIdField = value;
                this.RaisePropertyChanged("ReceiptId");
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:common", Order = 2)]
        public ErrorMessageDetailType ErrorMessageDetail
        {
            get
            {
                return this.errorMessageDetailField;
            }
            set
            {
                this.errorMessageDetailField = value;
                this.RaisePropertyChanged("ErrorMessageDetail");
            }
        }

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

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }

    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20")]
    public partial class ACABulkReqTrnsmtStsRespGrpDtlType : object, System.ComponentModel.INotifyPropertyChanged
    {

        private byte[] bulkExchangeFileField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:common", DataType = "base64Binary", Order = 0)]
        public byte[] BulkExchangeFile
        {
            get
            {
                return this.bulkExchangeFileField;
            }
            set
            {
                this.bulkExchangeFileField = value;
                this.RaisePropertyChanged("BulkExchangeFile");
            }
        }

        public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;

        protected void RaisePropertyChanged(string propertyName)
        {
            System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
            if ((propertyChanged != null))
            {
                propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
            }
        }
    }
}

谁能告诉我我做错了什么?谢谢

我认为这里有很多问题,更具体地说是 XML 的生成代码。

在 Visual Studio 中,我创建了一个新的 Class 并复制了您的 XML 内容并使用编辑 -> 选择性粘贴 -> 将 XML 粘贴为 Classes.

这是该练习生成的代码:

[System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:us:gov:treasury:irs:msg:irstransmitterstatusrequest")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:us:gov:treasury:irs:msg:irstransmitterstatusrequest", IsNullable = false)]
    public partial class ACABulkRequestTransmitterStatusDetailResponse
    {

        private ACABulkRequestTransmitterResponse aCABulkRequestTransmitterResponseField;

        private ACABulkReqTrnsmtStsRespGrpDtl aCABulkReqTrnsmtStsRespGrpDtlField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20")]
        public ACABulkRequestTransmitterResponse ACABulkRequestTransmitterResponse
        {
            get
            {
                return this.aCABulkRequestTransmitterResponseField;
            }
            set
            {
                this.aCABulkRequestTransmitterResponseField = value;
            }
        }

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20")]
        public ACABulkReqTrnsmtStsRespGrpDtl ACABulkReqTrnsmtStsRespGrpDtl
        {
            get
            {
                return this.aCABulkReqTrnsmtStsRespGrpDtlField;
            }
            set
            {
                this.aCABulkReqTrnsmtStsRespGrpDtlField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20", IsNullable = false)]
    public partial class ACABulkRequestTransmitterResponse
    {

        private string transmissionStatusCdField;

        private string receiptIdField;

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

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:common")]
        public string ReceiptId
        {
            get
            {
                return this.receiptIdField;
            }
            set
            {
                this.receiptIdField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:us:gov:treasury:irs:ext:aca:air:ty20", IsNullable = false)]
    public partial class ACABulkReqTrnsmtStsRespGrpDtl
    {

        private BulkExchangeFile bulkExchangeFileField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:common")]
        public BulkExchangeFile BulkExchangeFile
        {
            get
            {
                return this.bulkExchangeFileField;
            }
            set
            {
                this.bulkExchangeFileField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:us:gov:treasury:irs:common")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:us:gov:treasury:irs:common", IsNullable = false)]
    public partial class BulkExchangeFile
    {

        private Include includeField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.w3.org/2004/08/xop/include")]
        public Include Include
        {
            get
            {
                return this.includeField;
            }
            set
            {
                this.includeField = value;
            }
        }
    }

    /// <remarks/>
    [System.SerializableAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2004/08/xop/include")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.w3.org/2004/08/xop/include", IsNullable = false)]
    public partial class Include
    {

        private string hrefField;

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

使用你的反序列化方法和这个新的 Class 我能够成功解析你的 XML 文件。

我会使用此 Class 与您自己的生成代码进行比较,看看主要区别在哪里。除非您有能力替换生成的代码?

我能发现的第一个是需要将 ACABulkRequestTransmitterStatusDetailResponse 部分 class 上方的属性修改为:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.4084.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:us:gov:treasury:irs:msg:irstransmitterstatusrequest")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:us:gov:treasury:irs:msg:irstransmitterstatusrequest", IsNullable = false)]

希望对你有所帮助。

我发现了两个问题:

  1. 更改如下: 从 : [System.Xml.Serialization.XmlTypeAttribute(命名空间 = "urn:us:gov:treasury:irs:ext:aca:air:ty20")] 到 : [XmlRoot(Namespace = "urn:us:gov:treasury:irs:msg:irstransmitterstatusrequest")]

  2. 我更新这部分代码

         private byte[] BulkExchangeFileType;
    
         /// <remarks/>
         /// 
         //changed to string from bytes.
         [System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:us:gov:treasury:irs:common")]
         public object BulkExchangeFile
         {
             get
             {
                 return Encoding.UTF8.GetString(this.BulkExchangeFileType);
             }
             set
             {
                 this.BulkExchangeFileType = Encoding.UTF8.GetBytes(value.ToString());
                 this.RaisePropertyChanged("BulkExchangeFile");
             }
         }