markzzz
markzzz

Reputation: 47945

XDocument says Data at the root level is invalid?

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?

Upvotes: 1

Views: 4873

Answers (2)

I4V
I4V

Reputation: 35353

You should use XDocument.Load not XDocument.Parse. That would be because XDocument.Parse expects to receive an XML string, while you are trying to load it from a URL.

EDIT

You also have problem with XML namespaces. Try this

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

Upvotes: 3

Ajay
Ajay

Reputation: 6590

Try this.

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

Upvotes: 0

Related Questions