将httpWebResponse反序列化为对象

时间:2018-10-05 18:35:32

标签: c# httpresponse

我收到了网络回复,需要对列表进行消毒。我收到一个错误“根元素丢失”。有人可以告诉我如何解决。谢谢。

我调试代码并获取响应文本:

<ArrayOfLocation xmlns="http://schemas.datacontract.org/2004/07/Ordinging.Objects" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Location>
    <locationID>401</locationID>
    <locationName>Burnaby</locationName>
  </Location>
  <Location>
    <locationID>101</locationID>
    <locationName>Vancouver</locationName>
  </Location>
</ArrayOfLocation>

我要灭菌的代码:

using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {

            StreamReader reader = new StreamReader(response.GetResponseStream());                 
            result = reader.ReadToEnd();    
            XmlSerializer serializer = new XmlSerializer(typeof(List<LocationList.Location>));
            List<LocationList.Location> data = new List<LocationList.Location>();
            data = serializer.Deserialize(reader) as List<LocationList.Location>;

        }

我的应用中的位置信息类:

public class LocationList
{

    private List<Location> locations = null;
    [XmlElement("loctions")]
    public List<Location> locs
    {
        get { return locations; }
        set { locations = value; }
    }

    public class Location
    {
        public string locationName { get; set; }
        public Int64 locationID { get; set; }
        public Location(string name, Int64 id)
        {
            locationID = id;
            locationName = name;
        }
        public Location() { }

    }



}

1 个答案:

答案 0 :(得分:1)

一种方法。更改它以使用响应中的xml代替。我仅将硬编码的字符串用于测试)。

编辑:添加了帮助程序功能,可在需要时忽略名称空间。否则xml应该匹配名称空间。

using System;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace TestCodeApp {
    class TestCode {
        static void Main () {
            string xmlString = @"
<ArrayOfLocation xmlns='http://schemas.datacontract.org/2004/07/Ordinging.Objects' xmlns:i='http://www.w3.org/2001/XMLSchema-instance'>
  <Location>
    <locationID>401</locationID>
    <locationName>Burnaby</locationName>
  </Location>
  <Location>
    <locationID>101</locationID>
    <locationName>Vancouver</locationName>
  </Location>
</ArrayOfLocation>";

            StringReader stringReader = new StringReader (xmlString);
            XmlSerializer serializer = new XmlSerializer (typeof (List<Location>), new XmlRootAttribute ("ArrayOfLocation"));
            List<Location> locations = (List<Location>) serializer.Deserialize (new XmlTextReaderHelper(stringReader));

            foreach (Location item in locations) Console.WriteLine (item);
        }
    }

    public class XmlTextReaderHelper : XmlTextReader {
        public XmlTextReaderHelper (System.IO.TextReader reader) : base (reader) { }

        public override string NamespaceURI {
            get { return ""; }
        }
    }

    public class Location {
        public int locationID { get; set; }
        public string locationName { get; set; }
        public override string ToString () {
            return "ID: " + locationID + " - " + locationName;
        }
    }
}