2
我想從我的xml文件中獲取相關數據。因此,例如,我希望從場地元素中選擇來自元素Roker Park的所有數據。我該如何選擇這些數據而不是其餘部分?這是我的XDocument使用C#中的xdocument從xml文件中提取特定數據#
<Funrun>
<Venue>
<Name>Roker Park</Name>
<Runner>
<Charity>Cancer Research</Charity>
<Firstname>Roger</Firstname>
<Lastname>Malibu</Lastname>
<Sponsorship>550</Sponsorship>
</Runner>
<Runner>
<Charity>Arthritis UK</Charity>
<Firstname>Adam</Firstname>
<Lastname>White</Lastname>
<Sponsorship>340</Sponsorship>
</Runner>
<Venue>
<Name>Victoria Park</Name>
<Runner>
<Charity>Against Malaria Foundation</Charity>
<Firstname>Rob</Firstname>
<Lastname>Tate</Lastname>
<Sponsorship>850</Sponsorship>
</Runner>
<Runner>
<Charity>Fred Follows Foundation</Charity>
<Firstname>Peter</Firstname>
<Lastname>Jackson</Lastname>
<Sponsorship>500</Sponsorship>
</Runner>
</Venue>
</Funrun>
到目前爲止,我已經得到了代碼選擇所有場館的數據,所以我得到維多利亞公園以及Rokers,這是到目前爲止,我已經得到了代碼:
List<string> VenueNames = new List<string>();
List<string> VenueDataList = new List<string>();
var doc = XDocument.Load("XMLFile1.xml");
var doc2 = XDocument.Load("XMLFile2.xml");
var doc3 = XDocument.Load("XMLFile3.xml");
var combinedunique = doc.Descendants("Venue")
.Union(doc2.Descendants("Venue"))
.Union(doc3.Descendants("Venue"));
foreach (XElement element in combinedunique.DescendantsAndSelf("Venue"))
{
VenueNames.Add(element.Element("Name").Value.ToString());
}
if (Convert.ToString(lstboxVenues.SelectedItem) == VenueNames.ElementAt(0))
{
foreach (XElement element in combinedunique.DescendantsAndSelf("Firstname"))
{
VenueDataList.Add(element.Value);
}
for (int i = 0; i < VenueDataList.Count; i++)
{
lstboxVenueData.Items.Add(VenueDataList.ElementAt(i));
}
}
好像你沒有關閉'XML樣本 –
在這裏,在'標記是一個類似的答案,應該讓你你想要什麼http://stackoverflow.com/questions/752271/how-to-get-xml-node-from-xdocument –
你可以請澄清什麼數據從給定的XML你想要嗎? –