【发布时间】:2015-05-29 15:40:46
【问题描述】:
我在 xml 文件中有多个项目并且想要打印所有这些项目。但由于某种原因,它只打印 xml 文件中的第一项。
这是我的 xml 文件。存储在数据库表中名为 XmlFile 的列中:-
<root>
<item name="one" action="GetListOne" print="2" />
<item name="two" action="GetListTwo" print="1" />
<item name="three" action="GetListThree" print="2" />
<item name="four" action="GetListFour" print="0" />
</root>
这是我的模型类:-
public class ItemList
{
public string Name { get; set; }
public string Action { get; set; }
public string Print { get; set; }
}
和 Linq:-
var items = _repo.GetItemsById(Id).Single();
var xml = XDocument.Parse(items.XmlFile);
var model = from x in xml.Descendants("root")
select new ItemList
{
Name = x.Element("item").Attribute("name").Value,
Action = x.Element("item").Attribute("action").Value,
Print = x.Element("item").Attribute("print").Value,
};
foreach (var m in model)
{
Console.WriteLine(String.Format("Name = {0}",m.Name));
}
输出:-
名字 = 一个
【问题讨论】:
标签: c# asp.net linq asp.net-mvc-5