问题描述
我从服务中获取xml文件,但是当我想要
i get xml file from service , but i have problem when i want
Deserialize
来对象时,我有问题,总是显示
to object ,, always show me
root element is missing
xml
xml
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfResultInfo xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/">
<ResultInfo>
<PartNumber>KRPA-4007</PartNumber>
<Manufacturer>test</Manufacturer>
</ResultInfo>
</ArrayOfResultInfo>
code
code
string xml = null;
WebRequest req = WebRequest.Create(link);
req.Credentials = CredentialCache.DefaultCredentials;
WebResponse res = req.GetResponse();
Stream dataStream = res.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
xml = reader.ReadToEnd();
XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfResultInfo));
var result = (ArrayOfResultInfo)serializer.Deserialize(reader);
我的尝试:
string xml = null;
WebRequest req = WebRequest.Create(link);
req.Credentials = CredentialCache.DefaultCredentials;
WebResponse res = req.GetResponse();
流dataStream = res.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
xml = reader.ReadToEnd();
XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfResultInfo));
var result =(ArrayOfResultInfo)serializer.Deserialize(reader );
What I have tried:
string xml = null;
WebRequest req = WebRequest.Create(link);
req.Credentials = CredentialCache.DefaultCredentials;
WebResponse res = req.GetResponse();
Stream dataStream = res.GetResponseStream();
StreamReader reader = new StreamReader(dataStream);
xml = reader.ReadToEnd();
XmlSerializer serializer = new XmlSerializer(typeof(ArrayOfResultInfo));
var result = (ArrayOfResultInfo)serializer.Deserialize(reader);
推荐答案
string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ArrayOfResultInfo xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://tempuri.org/\"><ResultInfo><PartNumber>KRPA - 4007 </PartNumber><Manufacturer> test </Manufacturer></ResultInfo></ArrayOfResultInfo>";
var serializer = new XmlSerializer(typeof(ArrayOfResultInfo));
ArrayOfResultInfo result;
using (TextReader reader = new StringReader(xml))
{
result = (ArrayOfResultInfo)serializer.Deserialize(reader);
}
Console.WriteLine(result.ResultInfo.Manufacturer);
Console.WriteLine(result.ResultInfo.PartNumber);
Console.ReadLine();
xml如下:
The classes for xml are as follows:
[XmlRoot(ElementName = "ResultInfo", Namespace = "http://tempuri.org/")]
public class ResultInfo
{
[XmlElement(ElementName = "PartNumber", Namespace = "http://tempuri.org/")]
public string PartNumber { get; set; }
[XmlElement(ElementName = "Manufacturer", Namespace = "http://tempuri.org/")]
public string Manufacturer { get; set; }
}
[XmlRoot(ElementName = "ArrayOfResultInfo", Namespace = "http://tempuri.org/")]
public class ArrayOfResultInfo
{
[XmlElement(ElementName = "ResultInfo", Namespace = "http://tempuri.org/")]
public ResultInfo ResultInfo { get; set; }
[XmlAttribute(AttributeName = "xsd", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsd { get; set; }
[XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Xsi { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
}
希望这会有所帮助!
Hope this helps !
这篇关于反序列化XML,ASP时缺少根元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!