.net XmlDocument.load() 的根元素丢失错误

发布于 2024-07-17 13:01:21 字数 1398 浏览 8 评论 0原文

大家好。

当我将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

尾戒 2024-07-24 13:01:22

看起来 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...

⊕婉儿 2024-07-24 13:01:22

尝试使用 sr.ReadToEnd() 来查看返回给您的内容。 它可能是一个空字符串。

另外,如果您使用的是 .NET 2.0 或更高版本,则应该使用 XmlReader.CreateXmlTextReader 已弃用。

请参阅适用于 .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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文