从REST API XML反序列化到List< T>C# [英] Deserialization from REST API XML to List<T> C#

查看:82
本文介绍了从REST API XML反序列化到List< T>C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在写此问题之前,我尝试过方法.我花了整整一天的时间,却没有得到结果.

Before writing this question, I tried this, this and this method. I spent the whole day on it and could not get the result.

我有自己的REST API,该API在XML文件中返回序列化的 List< T>

I have my own REST API which returns a serialized List<T> in XML file

    [ActionName("planes")]
    [HttpGet]
    public IEnumerable<Planes> GetPlanes()
    {
        using (DB_A5vehiclesEntities entities = new DB_A5_vehiclesEntities())
        {
            return entities.Planes.ToList();
        }
    }

生成的XML文件如下所示(链接到xml文件):

Generated XML file looks like this (link to xml file):

<ArrayOfPlanes xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/VehicleDataAccess">
  <Planes>
    <FirstFlyYear>1932</FirstFlyYear>
    <Id>18</Id>
    <Name>18</Name>
    //other values
</Planes>
//other items

我的代码:

IList<Plane> myList = null;

        string xml = @"http://bo7145907-001-site2.ftempurl.com/wtvapi/vehicles/planes";
        XmlSerializer serializer = new XmlSerializer(typeof(List<Plane>));
        using (StringReader reader = new StringReader(xml))
        {

            myList = serializer.Deserialize(reader) as List<Plane>;
        }

        textMessage = FindViewById<TextView>(Resource.Id.message);

    }

我得到错误: System.Xml.XmlException:'根级别的数据无效.第1行,位置1

我只需要连接到API,反序列化XML并获取带有所有XML元素的 List< T> .怎么做对呢?

All I want is just connect to API, deserialize XML and get List<T> with all XML elements. How to do it right?

推荐答案

尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Linq;
using System.Net;
using System.Net.Sockets;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        const string URL = "http://bo7145907-001-site2.ftempurl.com/wtvapi/vehicles/planes";
        static void Main(string[] args)
        {

            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(URL);
            request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-US\r\n");
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36\r\n";
            request.Accept = "text/html,application/xhtml+xml,application/xml\r\n";

            WebResponse response = request.GetResponse();

            XmlReader xReader = XmlReader.Create(response.GetResponseStream());

            XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfPlanes));
            ArrayOfPlanes planes = (ArrayOfPlanes)serializer.Deserialize(xReader);
 
        }
    }
    [XmlRoot(ElementName = "ArrayOfPlanes", Namespace = "http://schemas.datacontract.org/2004/07/VehicleDataAccess")]
    public class ArrayOfPlanes
    {
        [XmlElement(ElementName = "Planes", Namespace = "http://schemas.datacontract.org/2004/07/VehicleDataAccess")]
        public List<Planes> Plains { get; set; }
    }
    public class Planes
    {
    }
}

这篇关于从REST API XML反序列化到List&lt; T&gt;C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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