0

我想yt:username从此XML中提取字段:

var xDoc = XDocument.Load(requestedURL);
var m_oListaMeteo = xDoc.Descendants(ns + "entry").Select(n =>
{
    return new
    {
        username = n.Element(ns + "yt:username").Value
    };
});

XDocument它本身说The ':' character, hexadecimal value 0x3A, cannot be included in a name.

需要字符串替换吗?还是需要我管理 youtube 的命名空间?

4

2 回答 2

2

yt is namespace, Try this:

var xDoc = XDocument.Load(@"https://gdata.youtube.com/feeds/api/users/djfonplaz/subscriptions?v=2");
var ns = XNamespace.Get("http://www.w3.org/2005/Atom");
var yt = XNamespace.Get("http://gdata.youtube.com/schemas/2007");
var m_oListaMeteo = xDoc.Descendants(ns + "entry").Select(n =>
{
    return new
    {
        username = n.Element(yt + "username").Value
    };
});
于 2013-08-22T14:08:39.373 回答
0

Make sure you are using the proper namespaces:

var xDoc = XDocument.Load(requestedURL);
var m_oListaMeteo = xDoc
    .Root
    .Elements("{http://www.w3.org/2005/Atom}entry")
    .Select(entry => new
    {
        username = entry.Element("{http://gdata.youtube.com/schemas/2007}username").Value
    });
于 2013-08-22T14:08:36.633 回答