Linq to XML 设置 arrayList 中的 XElements?
我正在尝试使用 ArrayList 设置 XElements,但遇到了一些麻烦。我基本上希望能够执行 foreach 循环,但不确定需要在哪里插入它。
ArrayList cities = new ArrayList();
foreach (ListItem item in lstCities.Items)
{
cities.Add(item.Text);
}
new XElement("Cities", cities //not sure what to do here
.Select(x=>new XElement("City",x)))
这不起作用,虽然它可以正常工作,但我想要城市名称,而不是数组编号
new XElement("Countries", lstCountry.GetSelectedIndices()
.Select(x => new XElement("Country", x))
I am trying to set XElements with an ArrayList and having a bit of trouble. I basically want to be able to do a foreach loop, but not sure where I need to insert it.
ArrayList cities = new ArrayList();
foreach (ListItem item in lstCities.Items)
{
cities.Add(item.Text);
}
new XElement("Cities", cities //not sure what to do here
.Select(x=>new XElement("City",x)))
This does not work, though it worked okay with this, but I want the city names, not array number
new XElement("Countries", lstCountry.GetSelectedIndices()
.Select(x => new XElement("Country", x))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您为什么要使用
ArrayList
而不是List
来开始?如果您被迫使用
ArrayList
,那么您可以这样做:...但是您最好使用
List
,如果可能的。或者:
Any reason why you're using an
ArrayList
instead of aList<string>
to start with?If you're forced to use
ArrayList
then you could do:... but you'd be better off using
List<string>
if possible.Alternatively: