说的XDocument在根级别的数据是无效的? [英] XDocument says Data at the root level is invalid?

查看:469
本文介绍了说的XDocument在根级别的数据是无效的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析XML,这是在这个的网址

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);
}

但它与回复的数据在根级别无效。行1,位置1。

我在哪里错了?

推荐答案

您应该使用 XDocument.Load 不是 XDocument.Parse 。这将是因为 XDocument.Parse 需要接收一个XML字符串,而你正试图从一个URL加载它。

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.

修改

您也有问题,XML命名空间。试试这个

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

这篇关于说的XDocument在根级别的数据是无效的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆