[英]Output the XML-File into a Listview
i want to read a XML-File and load the Content in a listview.我想读取 XML 文件并将内容加载到列表视图中。
At first the XML-File:首先是 XML 文件:
<?xml version="1.0"?>
<Budget_Table>
<budgets>
<Einnahmen>10€</Einnahmen>
<Anmerkung_Einnahme>Win</Anmerkung_Einnahme>
<Ausgaben>6€</Ausgaben>
<Anmerkung_Ausgabe>Pizza</Anmerkung_Ausgabe>
<Datum>12.03.2011</Datum>
</budgets>
<budgets>
<Einnahmen></Einnahmen>
<Anmerkung_Einnahme></Anmerkung_Einnahme>
<Ausgaben>6€</Ausgaben>
<Anmerkung_Ausgabe>Pizza</Anmerkung_Ausgabe>
<Datum>20.03.2011</Datum>
</budgets>
</Budget_Table>
Ok this is the XML-File.好的,这是 XML 文件。 In the Rows is the Content.
在行中是内容。 The App should be something like a Budget Book.
该应用程序应该类似于预算书。
Now the Listview and the whole Problem... I dont know how i should realise the Code.现在 Listview 和整个问题......我不知道我应该如何实现代码。 Should i use a Dataset, Datatables, Linq?
我应该使用数据集、数据表、Linq 吗? Whats good?
有什么好的?
At the end i wand to edit the Datas, delete and add.最后我想编辑数据,删除和添加。 So i have to write to the xml, too.
所以我也必须写信给 xml。
I can do:我可以:
DataSet dataset= new DataSet();
dataset.ReadXml("budget.xml");
But then how can i read the TAGs?但是,我怎样才能阅读标签? , ... and all these are the Columns in the Listview.
, ... 所有这些都是 Listview 中的列。 How can I use them right?
我怎样才能正确使用它们?
Hope you can give me some help.希望你能给我一些帮助。
Create a model (class) for a budget:为预算创建 model(类):
public class Budget{
public string Einnahmen {get; set;}
public string AnmerkungEinnahme {get; set;}
public string Ausgaben {get; set;}
public string AnmerkungAusgabe {get; set;}
public DateTime Datum {get; set;}
}
Then, use Linq to populate a collection of Budget items:然后,使用 Linq 填充预算项目的集合:
public static List<Budget> GetBudgets()
{
XDocument data = XDocument.Load(HttpContext.Current.Server.MapPath("~/Data/Budgets.xml"));
return (from b in data.Descendants("Budget")
orderby b.Attribute("Datum")
select new Budget()
{
Einnahmen = b.Element("Einnahmen").Value,
AnmerkungEinnahme= b.Element("Anmerkung_Einnahme").Value,
Ausgaben = b.Element("Ausgaben").Value,
AnmerkungAusgabe= b.Element("Anmerkung_Ausgabe").Value,
Datum = b.Element("Datum").Value
}).ToList();
}
You can now bind your listview to this object and use the <%# Eval("Einnahmen")%>
construct to put out the right data.您现在可以将您的列表视图绑定到此 object 并使用
<%# Eval("Einnahmen")%>
构造来输出正确的数据。
Hope this helps.希望这可以帮助。 Chris.
克里斯。
you can select what you want to using select
method from your dataset like你可以 select 你想从你的数据集中使用
select
方法
dataset.Tables["budgets"].Select("where Anmerkung_Einnahme=Win");
or you can use XPath to query your XML file by loading it into XmlDocument
Object或者您可以使用 XPath 来查询您的 XML 文件,方法是将其加载到
XmlDocument
Object
XmlDocument doc = new XmlDocument();
doc.Load("FullPath");
XmlNode = doc.SelectNodes(@"/Budget_Table/budgets[Anmerkung_Einnahme=Win]");
these two example will return all budegts nodes with Anmerkung_Einnahme=Win;这两个示例将返回 Anmerkung_Einnahme=Win 的所有预算节点;
to update your xml you can set the properties of the any node you selected and then save the XmlDocument
into file要更新您的 xml 您可以设置您选择的任何节点的属性,然后将
XmlDocument
保存到文件中
doc.Save("FullPath");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.