【问题标题】:Why XDocument.Load(url) throws exception?为什么 XDocument.Load(url) 抛出异常?
【发布时间】:2013-01-29 05:20:39
【问题描述】:

我是 C# 新手,我正在尝试从 URL 读取 xml。 xml 是这样的

<posts>
   <post>
      <title>title1</title>
      <des>des1</des>
   </post>
   <post>
      <title>title2</title>
      <des>des2</des>
   </post>
 .....
</posts>

这就是我用来解析它的。

 String uri = "url";
 XDocument books = XDocument.Load(uri);

当调试命中XDocument 行时,它会抛出异常并跳过它。

我怎样才能避免这种情况?

【问题讨论】:

  • 尝试使用 String uri = "url.xml";如果这有帮助,请告诉我
  • 什么异常???请在您的问题中发布异常的详细信息和堆栈跟踪

标签: c# xml linq windows-phone-7 url-parsing


【解决方案1】:

我认为您的 XML 的 URI 缺少导致问题的文件扩展名。请尝试使用:

String uri = PATH + "url.xml"; 
XDocument books = new XDocument();
books.Load(uri);

要解析从 URL 获得的 XML,你可以使用:

string strURL = "http://<some-server>/<some-uri-path>";
string xmlStr;

WebClient wc = new WebClient();
xmlStr = wc.DownloadString(strURL);

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlStr);

【讨论】:

  • 我正在使用 php 创建 xml,所以 url 看起来像这样 google.com?search=test
  • 我在上面添加了修改后的代码。你能检查一下它是否适合你的要求
  • 所以我得到了这个“System.Net.Webclient:在 using 语句中使用的类型必须隐式转换为 System.Idisposable。并且 System.Net.Webclient 不包含 DownloadString 的定义
  • 删除 using 块后,它应该给出 IDisposable 的错误。我还是尽力了。它没有给出任何错误,它也有 DownloadString 方法。
  • 你使用了哪些命名空间?
猜你喜欢
  • 2010-12-09
  • 2011-01-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-28
  • 2011-12-12
  • 2013-09-04
  • 1970-01-01
相关资源
最近更新 更多