1

I'm trying to parse an XML, which is at this url:

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

but it replies with Data at the root level is invalid. Line 1, position 1.

Where am I wrong?

4

2 回答 2

2

你应该使用XDocument.Loadnot XDocument.Parse。那是因为XDocument.Parse当您尝试从 URL 加载它时,期望接收 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

于 2013-08-22T13:32:24.317 回答
0

尝试这个。

try
{
  var xDoc = XDocument.Load(requestedURL);
  Response.Write("asd: " + xDoc.Descendants("entry").Count());
}
catch (Exception err)
{
  Response.Write(err.Message);
} 
于 2013-08-22T13:33:58.173 回答