2013-08-22 28 views
0

我想从this XML提取场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的命名空间?

回答

2

yt是命名空间,试试这个:

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

确保你使用了正确的命名空间:

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