在windowsphone 8中搜索xml文档的元素
本文关键字:文档 元素 xml 搜索 windowsphone | 更新日期: 2023-09-27 18:29:34
我有一个xml文档,我正试图查询并将数据链接到windowsphone 8中的列表框,但到目前为止我失败了。网上的例子似乎并没有带来我想要的东西。以下是我目前所拥有的。在xml中。这是我的xml文档(mydata.cml)
<resources>
<songCover>
<item>Images/song1</item>
<item>Images/song2</item>
.....
</songCover>
<songName>
<item>song1</item>
<item>song2</item>
.....
</songName>
<releaseYear>
<item>2015</item>
<item>2014</item>
.....
</releaseYear>
和c#
XDocument doc=XDocument.Load("mydata.xml");
var data1=from Q in doc.Descedants("songCover") select new{Item=Q.Element("item").Value;
var data2=from Q in doc.Descedants("songName") select new{Item=Q.Element("item").Value;
var data3=from Q in doc.Descedants("releaseYear") select new{Item=Q.Element("item").Value;
foreach(var x in data1)
foreach(var y in data2)
foreach(var z in data3)
//create a list of songs with there data eg [songCover1,song1,2015],[songCover2,song2,2014] etc
这是我的xaml
<longlistselector>
......
<stackPanel>
<image height=”100” width=”100” source={Binding data1} horizontal-alignment=left/>
<textblock horizontal-alignment=right text={Binding data 2}/>
<texblock horizontal-alignment=right text={Binding data 3}/>
</stackPanel>
......
</longlistselector>
提前谢谢。
假设每个集合中的项目数(songCovers、songNames和releaseYears)相同,并且它们按索引对应,则可以这样做:
XDocument doc = XDocument.Load("mydata.xml");
var data1 = doc.Descedants("songCover")
.Elements("item")
.Select(o => o.Value)
.ToList();
var data2 = doc.Descedants("songName")
.Elements("item")
.Select(o => o.Value)
.ToList();
var data3 = doc.Descedants("releaseYear")
.Elements("item")
.Select(o => o.Value)
.ToList();
for(int i=0; i<data1.Count; i++)
{
var songCover = data1[i];
var songName = data2[i];
var releaseYear = data3[i];
//combine the 3 values above as needed
}
这就是您想要的吗?。我假设(根据您在摘录中的评论)封面、姓名和年份分别位于同一位置。
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("path_here");
XmlNodeList covers = xmlDoc.SelectNodes("/songCover/item");
XmlNodeList names = xmlDoc.SelectNodes("/songName/item");
XmlNodeList years = xmlDoc.SelectNodes("/releaseYear/item");
for(int i=0;i<covers.Count; i++)
{
Console.WriteLine("[ "+ covers[i].Value +", " + names[i].Value + ", "+ years[i].Value + " ]");
}
XDocument xdoc = XDocument.Load("file_path_here");
List<xElement> covers = xDoc.XPathSelectElements("/songCover/item").ToList();
List<xElement> names = xDoc.XPathSelectElements("/songName/item").ToList();
List<xElement> years = xDoc.XPathSelectElements("/releaseYear/item").ToList();
for (int i = 0; i < covers.Count; i++)
{
Console.WriteLine("[ " + covers[i].Value + ", " + names[i].Value + ", " + years[i].Value + " ]");
}