XDocument说根级别的数据无效?

时间:2013-08-22 13:31:00

标签: c# .net xml linq-to-xml deserialization

我正在尝试解析XML,这是url

try
{
    var xDoc = XDocument.Parse(requestedURL);
    Response.Write("asd: " + xDoc.Descendants("entry").Count());
}
catch (Exception err)
{
    Response.Write(err.Message);
}

但它回复Data at the root level is invalid. Line 1, position 1.

我哪里错了?

2 个答案:

答案 0 :(得分:2)

您应该使用XDocument.Load而不是XDocument.Parse。这是因为XDocument.Parse期望在您尝试从URL加载XML字符串时收到XML字符串。

修改

您还遇到XML命名空间问题。试试这个

var xDoc = XDocument.Load(requestedURL);
XNamespace ns = "http://www.w3.org/2005/Atom";
var count = xDoc.Descendants(ns + "entry").Count();

http://msdn.microsoft.com/en-us/library/bb343181.aspx

答案 1 :(得分:0)

试试这个。

try
{
  var xDoc = XDocument.Load(requestedURL);
  Response.Write("asd: " + xDoc.Descendants("entry").Count());
}
catch (Exception err)
{
  Response.Write(err.Message);
}