.net XmlDocument.load() 的根元素丢失错误
大家好。
当我将 REST 请求的结果加载到 XmlDocument 中时,出现 asp.net 错误“根元素丢失”。 当我使用 Firefox 插件“RESTTEST”执行相同的 REST 请求时,它看起来不错并返回有效结果。 但错误出现在 C#.net 代码隐藏中。 有谁知道这可能是什么原因造成的? 以下是相关代码:
HttpWebResponse response = null;
response = (HttpWebResponse)request.GetResponse();
HttpStatusCode statusCode = response.StatusCode;
Stream responseData = response.GetResponseStream();
StreamReader sr = new StreamReader(responseData);
XmlTextReader reader = new XmlTextReader(sr);
XmlDocument doc = new XmlDocument();
doc.Load(sr); // here is where the error occurs.
我的目标是将 REST 请求的结果加载到可遍历的 XML 数据模型中,然后我可以从中获取元素及其值。
当我使用这段代码时,我得到了预期的结果。 有什么不同?
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an Element.
Response.Write("Element Name: " + reader.Name);
while (reader.MoveToNextAttribute()) // Read attributes.
Response.Write(" " + reader.Name + "='" + reader.Value + "'");
Response.Write("<br />");
break;
case XmlNodeType.Text: //Display the text in each element.
Response.Write("Element value: " + reader.Value);
Response.Write("Read key=" + reader.Name + ", value=" + reader.Value + "<br/>");
break;
case XmlNodeType.EndElement: //Display end of element.
Response.Write("<br />");
break;
}
}
Greetings all.
I'm getting an asp.net error "Root element is missing" when I load the results of a REST request into an XmlDocument. The same REST request looks fine and returns valid results when I execute it using the Firefox addon "RESTTEST". But the error shows up in the C#.net code-behind. Does anyone know what might cause this? Here is the relevant code:
HttpWebResponse response = null;
response = (HttpWebResponse)request.GetResponse();
HttpStatusCode statusCode = response.StatusCode;
Stream responseData = response.GetResponseStream();
StreamReader sr = new StreamReader(responseData);
XmlTextReader reader = new XmlTextReader(sr);
XmlDocument doc = new XmlDocument();
doc.Load(sr); // here is where the error occurs.
My goal is to load the results of the REST request into a traverse-able XML data model which I can then grab elements and their values from.
When I use this code, I get the expected results. What is the difference?
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an Element.
Response.Write("Element Name: " + reader.Name);
while (reader.MoveToNextAttribute()) // Read attributes.
Response.Write(" " + reader.Name + "='" + reader.Value + "'");
Response.Write("<br />");
break;
case XmlNodeType.Text: //Display the text in each element.
Response.Write("Element value: " + reader.Value);
Response.Write("Read key=" + reader.Name + ", value=" + reader.Value + "<br/>");
break;
case XmlNodeType.EndElement: //Display end of element.
Response.Write("<br />");
break;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看起来 XML 是一个片段,而不是一个完整的 XML 文档 —— 这就是它没有根的原因。 要使其正常工作,您必须配置 XMLDocument 对象以接受片段...
It looks like the XML is a fragment rather than a fully formed XML document-- that's why it didn't have the root. To get this to work you have to configure the XMLDocument object to accept fragments...
尝试使用 sr.ReadToEnd() 来查看返回给您的内容。 它可能是一个空字符串。
另外,如果您使用的是 .NET 2.0 或更高版本,则应该使用
XmlReader.Create
;XmlTextReader
已弃用。请参阅适用于 .NET 的 REST 客户端库,第 1 部分使用 XML 序列化的示例。 (抱歉,没有第 2 部分)。
Try using
sr.ReadToEnd()
to see what's being returned to you. It's probably an empty string.Also, you should be using
XmlReader.Create
if you're using .NET 2.0 or above;XmlTextReader
is deprecated.See A REST Client Library for .NET, Part 1 for an example that uses XML Serialization. (sorry, there is no part 2).