C# (element.Attribute(“driverId”).Value)).ToList();
编辑:或使用命名空间:C# (element.Attribute(“driverId”).Value)).ToList();,c#,xml,linq,windows-phone-7,C#,Xml,Linq,Windows Phone 7,编辑:或使用命名空间: var ns = xmlEntries.Root.Name.Namespace; drivers = (from element in xmlEntries.Root.Element(ns + "DriverTable").Descendants(ns + "Driver") select new Driver(element.Attribute("driverId").Value)).ToList<Driver>(); var ns=
var ns = xmlEntries.Root.Name.Namespace;
drivers = (from element in xmlEntries.Root.Element(ns + "DriverTable").Descendants(ns + "Driver")
select new Driver(element.Attribute("driverId").Value)).ToList<Driver>();
var ns=xmlcentries.Root.Name.Namespace;
drivers=(来自xmlEntries.Root.element(ns+“DriverTable”).subjects(ns+“Driver”)中的元素)
选择新驱动程序(element.Attribute(“driverId”).Value)).ToList();
试试这个
private void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
return;
}
XDocument xmlEntries = XDocument.Parse(e.Result);
IEnumerable<Driver> list = from item in feed.Descendants("channel").Elements("Driver")
select new Driver
{
Name = (string)item.Attribute("driverId")
};
ObservableCollection<Driver>() collection = new ObservableCollection<Driver>();
foreach (Driver d in list)
{
collection.Add(d);
}
DriverListBox.ItemsSource = collection;
}
private void webclient\u DownloadStringCompleted已完成(对象发送方,DownloadStringCompletedEventArgs e)
{
如果(例如错误!=null)
{
返回;
}
XDocument xmlEntries=XDocument.Parse(e.Result);
IEnumerable list=来自feed.subscriptions(“通道”).Elements(“驱动程序”)中的项
选择新驱动程序
{
Name=(字符串)item.Attribute(“driverId”)
};
ObservableCollection()集合=新的ObservableCollection();
foreach(列表中的驱动程序d)
{
增加(d);
}
DriverListBox.ItemsSource=集合;
}
我得到了一个nullreference异常,它处理属性,但不处理元素。我不知道Name.Namespace。那会从我以前的项目中删除很多硬编码的垃圾!Cool你说它与属性一起工作,而不是与元素一起工作是什么意思??你想得到什么元素?
drivers = (from element in xmlEntries.Descendants()
where element.Name.LocalName == "Driver"
select new Driver(element.Attribute("driverId").Value)).ToList<Driver>();
var ns = xmlEntries.Root.Name.Namespace;
drivers = (from element in xmlEntries.Root.Element(ns + "DriverTable").Descendants(ns + "Driver")
select new Driver(element.Attribute("driverId").Value)).ToList<Driver>();
private void webclient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
return;
}
XDocument xmlEntries = XDocument.Parse(e.Result);
IEnumerable<Driver> list = from item in feed.Descendants("channel").Elements("Driver")
select new Driver
{
Name = (string)item.Attribute("driverId")
};
ObservableCollection<Driver>() collection = new ObservableCollection<Driver>();
foreach (Driver d in list)
{
collection.Add(d);
}
DriverListBox.ItemsSource = collection;
}