2013-08-22 61 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 
    });