C# 将httpWebResponse反序列化为对象

C# 将httpWebResponse反序列化为对象,c#,httpresponse,C#,Httpresponse,我收到了网络回复,需要在列表中取消消毒。我得到一个错误“根元素丢失”。有人能告诉我怎么解决吗。谢谢 我调试代码并获得响应文本: <ArrayOfLocation xmlns="http://schemas.datacontract.org/2004/07/Ordinging.Objects" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <Location> <locationID>401

我收到了网络回复,需要在列表中取消消毒。我得到一个错误“根元素丢失”。有人能告诉我怎么解决吗。谢谢

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

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

401
本纳比
101
温哥华
我要去灭菌的代码:

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

        }
使用(HttpWebResponse=request.GetResponse()作为HttpWebResponse)
{
StreamReader=新的StreamReader(response.GetResponseStream());
结果=reader.ReadToEnd();
XmlSerializer serializer=新的XmlSerializer(typeof(List));
列表数据=新列表();
数据=序列化程序。反序列化(读取器)为列表;
}
我的应用程序中的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() { }

    }



}
公共类位置列表
{
私有列表位置=空;
[XmlElement(“位置”)]
公开名单LOC
{
获取{返回位置;}
设置{locations=value;}
}
公共类位置
{
公共字符串位置名称{get;set;}
公共Int64 locationID{get;set;}
公共位置(字符串名称,Int64 id)
{
locationID=id;
locationName=名称;
}
公共位置(){}
}
}

一种方法。将其更改为使用响应中的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;
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Xml;
使用System.Xml.Serialization;
命名空间TestCodeApp{
类测试代码{
静态空隙干管(){
字符串xmlString=@”
401
本纳比
101
温哥华
";
StringReader StringReader=新的StringReader(xmlString);
XmlSerializer serializer=新的XmlSerializer(typeof(List),新的XmlRootAttribute(“ArrayFlocation”);
列表位置=(列表)序列化程序。反序列化(新的XmlTextReaderHelper(stringReader));
foreach(位置中的位置项)Console.WriteLine(项);
}
}
公共类XmlTextReaderHelper:XmlTextReader{
公共XmlTextReaderHelper(System.IO.TextReader阅读器):基本(阅读器){}
公共重写字符串NamespaceURI{
获取{return”“;}
}
}
公共类位置{
public int locationID{get;set;}
公共字符串位置名称{get;set;}
公共重写字符串ToString(){
返回“ID:”+locationID+“-”+locationName;
}
}
}

序列化程序不喜欢您的默认命名空间。您可以控制xml生成吗?添加任何前缀(从xmlns=到xmlns:x=)都会起作用。您能说一下xml是如何创建的吗?另外,代码中的位置可能有误-[xmlement(“locations”)]OP说响应中的xml就是显示的内容。如果OP可以删除名称空间,那么现有代码就可以工作。编辑我的答案,以显示如果OP没有选择,如何忽略名称空间。