我有一個問題,即時通訊嘗試從URL的xml文件的屬性中獲取值。c#無法從xml獲取屬性
XML:http://thegamesdb.net/api/GetGamesList.php?name=x-men
代碼:
public MainPage()
{
InitializeComponent();
var webClient = new WebClient();
webClient.DownloadStringCompleted += RequestCompleted;
webClient.DownloadStringAsync(new Uri("http://thegamesdb.net/api/GetGamesList.php?name=x-men"));
}
private void RequestCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
var feedXml = XDocument.Parse(e.Result);
var gameData = feedXml.Root.Elements("Game").Select(x => new GetGamesList
{
// ERROR VALUES ARE NULL
ID = (int)x.Attribute("id"),
GameTitle = (string)x.Attribute("GameTitle"),
ReleaseDate = (string)x.Attribute("ReleaseDate"),
Platform = (string)x.Attribute("Platform")
})
.ToList();
}
}
public class GetGamesList
{
public int ID { get; set; }
public string GameTitle { get; set; }
public string ReleaseDate { get; set; }
public string Platform { get; set; }
}
我希望有一個人可以幫助我,謝謝。
你想要得到一個元素? – bubbinator
是的,現在我只是想獲得其中的一個元素。 – Thunder