将 XML 文件反序列化为 C# 类对象时出错

Posted

技术标签:

【中文标题】将 XML 文件反序列化为 C# 类对象时出错【英文标题】:Error deserializing XML file into C# class of objects 【发布时间】:2021-11-01 15:10:19 【问题描述】:

我有这个 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<pippo:Response xmlns:pippo="http://pippoonboarding.eu">
  <pippo:Header>
    <pippo:FileId>123</pippo:FileId>
    <pippo:SenderId>1234</pippo:SenderId>
    <pippo:ProcessingDate>20210630</pippo:ProcessingDate>
    <pippo:ProcessingTime>1130</pippo:ProcessingTime>
    <pippo:ResponseCode>OK</pippo:ResponseCode>
  </pippo:Header>
  <pippo:CompanyResponse>
    <pippo:SellerId>1234</pippo:SellerId>
    <pippo:SellerContractCode />
    <pippo:VATNumber>123456</pippo:VATNumber>
    <pippo:ResponseCode>KO</pippo:ResponseCode>
    <pippo:PippoCompanyCode />
    <pippo:ResponseDetails>
      <pippo:Entity>ciaone</pippo:Entity>
      <pippo:ProgressiveNumber>1</pippo:ProgressiveNumber>
      <pippo:PippoShopCode />
      <pippo:TerminalId />
      <pippo:FieldName />
      <pippo:ErrorType>DDD</pippo:ErrorType>
      <pippo:ErrorCode>1234</pippo:ErrorCode>
      <pippo:ErrorDescription>test</pippo:ErrorDescription>
    </pippo:ResponseDetails>
  </pippo:CompanyResponse>
</pippo:Response>

我想反序列化到我的班级:

public class XmlDeserializer

    [Serializable, XmlRoot("pippo:Response xmlns:pippo=\"http://pippoonboarding.eu\"")]
    public class Root
    
        public string Response  get; set; 

        //[XmlElement(ElementName = "Header")]
        public Header Header  get; set; 

        public CompanyResponse CompanyResponse  get; set; 

    

    public class Header
    
        public string FileId  get; set; 
        public string SenderId  get; set; 
        public string ProcessingDate  get; set; 
        public string ProcessingTime  get; set; 
        public string ResponseCode  get; set; 

    

    public class CompanyResponse
    
        public string SellerId  get; set; 
        public int SellerContractCode  get; set; 
        public int VATNumber  get; set; 
        public int ResponseCode  get; set; 
        public int PippoCompanyCode  get; set; 
        public ResponseDetails ResponseDetails  get; set; 

    

    public class ResponseDetails
    
        public string Entity  get; set; 
        public string ProgressiveNumber  get; set; 
        public string PippoShopCode  get; set; 
        public string TerminalId  get; set; 
        public string FieldName  get; set; 
        public string ErrorType  get; set; 
        public string ErrorCode  get; set; 
        public string ErrorDescription  get; set; 

    

但我收到此错误:

XML 文档 (2, 2) 中存在错误。 &lt;Response xmlns='http://pippoonboarding.eu'&gt; 不是预期的。

错误是什么意思?我该怎么办?

【问题讨论】:

【参考方案1】:

以下代码有效。必须在类定义中将一些整数更改为字符串。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication2

    class Program
    
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        
            XmlDeserializer response = new XmlDeserializer(FILENAME);

        
    
    public class XmlDeserializer
    
        public XmlDeserializer(string filename)
        
            XmlReader reader = XmlReader.Create(filename);
            XmlSerializer serializer = new XmlSerializer(typeof(Root));
            Root response = (Root)serializer.Deserialize(reader);
        

        [XmlRoot(ElementName = "Response", Namespace = "http://pippoonboarding.eu")]
        public class Root
        
            public string Response  get; set; 
            //[XmlElement(ElementName = "Header")]
            public Header Header  get; set; 
            public CompanyResponse CompanyResponse  get; set; 
        
        public class Header
        
            public string FileId  get; set; 
            public string SenderId  get; set; 
            public string ProcessingDate  get; set; 
            public string ProcessingTime  get; set; 
            public string ResponseCode  get; set; 
        
        public class CompanyResponse
        
            public string SellerId  get; set; 
            public string SellerContractCode  get; set; 
            public int VATNumber  get; set; 
            public string ResponseCode  get; set; 
            public string PippoCompanyCode  get; set; 
            public ResponseDetails ResponseDetails  get; set; 
        
        public class ResponseDetails
        
            public string Entity  get; set; 
            public string ProgressiveNumber  get; set; 
            public string PippoShopCode  get; set; 
            public string TerminalId  get; set; 
            public string FieldName  get; set; 
            public string ErrorType  get; set; 
            public string ErrorCode  get; set; 
            public string ErrorDescription  get; set; 
        
    

  

【讨论】:

以上是关于将 XML 文件反序列化为 C# 类对象时出错的主要内容,如果未能解决你的问题,请参考以下文章

需要帮助将 XML 文件反序列化为对象 C#

将 Xml 反序列化为对象时出错 - xmlns='' 不是预期的

将 Xml 反序列化为对象时出错 - xmlns='' 不是预期的

XML 文档中存在错误 (0, 0) - 将 XML 反序列化为对象时出错

将字符串反序列化为 *.proto 中定义的对象时出错?

如何将 XML 反序列化为 C# 中的对象? [复制]