将 httpWebResponse 反序列化为对象
Deserialize httpWebResponse into object
收到网页回复,需要除菌进入列表。我收到一个错误 "Root element is missing"。有人会告诉我如何解决它。谢谢
我调试代码并得到响应文本:
<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>;
}
我的应用程序中的位置 Class:
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() { }
}
}
一种方法。将其更改为使用您的回复中的 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;
}
}
}
收到网页回复,需要除菌进入列表。我收到一个错误 "Root element is missing"。有人会告诉我如何解决它。谢谢
我调试代码并得到响应文本:
<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>;
}
我的应用程序中的位置 Class:
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() { }
}
}
一种方法。将其更改为使用您的回复中的 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;
}
}
}