将 XML 文件反序列化为 C# 类对象时出错 [英] Error deserializing XML file into C# class of objects

查看:27
本文介绍了将 XML 文件反序列化为 C# 类对象时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个 xml 文件:

I have this xml file:

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

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

and I want to deserialize into my class:

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) 中存在错误.<响应 xmlns='http://pippoonboarding.eu'> 不是预期的.

There is an error in XML document (2, 2). <Response xmlns='http://pippoonboarding.eu'> was not expected.

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

What does the error mean? What should I do?

推荐答案

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

Following code works. Had to change a few integers to strings in class definitions.

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# 类对象时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆