Xmlexcpetion:根级别的数据无效。第1行位置1 [英] Xmlexcpetion: data at the root level is invalid. Line 1 position 1

查看:113
本文介绍了Xmlexcpetion:根级别的数据无效。第1行位置1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从服务获取响应,这是一个xml响应。我收到一个xml异常根级别的数据无效



我尝试了什么:



这是我获取http响应的代码:

public XmlDocument ReceiveXMLResponse()

{

string retVal = string.Empty;

XmlDocument xmlDoc = new XmlDocument();



var httpResponse =(HttpWebResponse)_httpWebRequest.GetResponse();



if(httpResponse.StatusCode == HttpStatusCode.OK)

{

using(var streamReader = new StreamReader(httpResponse.GetResponseStream(),Encoding.GetEncoding(ISO-8859-1)))

{

retVal = streamReader.ReadtoEnd();

xmlDoc.LoadXml(retVal);

else

{



retVal = httpResponse。统计usCode.ToString();

}



返回xmlDoc;

}



以下是我获取元素值的方法:



XmlNodeList firstName = CertificateInfo.GetElementsByTagName(FirstName);

string FirstName = string.Empty;

foreach(firstName中的XmlNode节点)

{

FirstName = node.InnerText;

}

I am trying to get the response from a service, which is an xml response. I am receiving an xml exception "Data at the root level is invalid"

What I have tried:

Here is my code to get the http response:
public XmlDocument ReceiveXMLResponse()
{
string retVal = string.Empty;
XmlDocument xmlDoc = new XmlDocument();

var httpResponse = (HttpWebResponse)_httpWebRequest.GetResponse();

if (httpResponse.StatusCode == HttpStatusCode.OK)
{
using (var streamReader = new StreamReader(httpResponse.GetResponseStream(), Encoding.GetEncoding("ISO-8859-1")))
{
retVal = streamReader.ReadtoEnd();
xmlDoc.LoadXml(retVal);
else
{

retVal = httpResponse.StatusCode.ToString();
}

return xmlDoc;
}

Here is how I am getting the element values:

XmlNodeList firstName = CertificateInfo.GetElementsByTagName("FirstName");
string FirstName = string.Empty;
foreach (XmlNode node in firstName)
{
FirstName = node.InnerText;
}

推荐答案

很可能你没有获得XML响应,但可能是JSON repsonse。你必须在 retVal 变量中查看你得到的内容才能找到答案。



这是学习的地方使用调试器非常方便。
Chances are good that you didn't get an XML response, but possibly a JSON repsonse. You have to look at what you got back in your retVal variable to find out.

This is where learning to use the debugger comes in really handy.


这篇关于Xmlexcpetion:根级别的数据无效。第1行位置1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆