xmlns=''> was not expected. - There is an

2019-04-03 23:14发布

Im trying to deserialize the response from this simple web service

Im using the following code:

WebRequest request = WebRequest.Create("http://inb374.jelastic.tsukaeru.net:8080/VodafoneDB/webresources/vodafone/04111111");    
WebResponse ws = request.GetResponse();
XmlSerializer s = new XmlSerializer(typeof(string));
string reponse = (string)s.Deserialize(ws.GetResponseStream());

2条回答
混吃等死
2楼-- · 2019-04-03 23:40

You want to deserialize the XML and treat it as a fragment.

There's a very straightforward workaround available here. I've modified it for your scenario:

var webRequest = WebRequest.Create("http://inb374.jelastic.tsukaeru.net:8080/VodafoneDB/webresources/vodafone/04111111");

using (var webResponse = webRequest.GetResponse())
using (var responseStream = webResponse.GetResponseStream())
{
    var rootAttribute = new XmlRootAttribute();
    rootAttribute.ElementName = "response";
    rootAttribute.IsNullable = true;

    var xmlSerializer = new XmlSerializer(typeof (string), rootAttribute);
    var response = (string) xmlSerializer.Deserialize(responseStream);
}
查看更多
爱情/是我丢掉的垃圾
3楼-- · 2019-04-03 23:47

Declaring XmlSerializer as

XmlSerializer s = new XmlSerializer(typeof(string),new XmlRootAttribute("response"));

is enough.

查看更多
登录 后发表回答