覺得這是我的XML ..如何獲得XML的父元素的所有子元素在C#
<ListOfDestinations>
<Destination>
<City>Ahmedabad</City>
<Title>Cheap Flights to Ahmedabad</Title>
<Content>
<Top10PlacestoVisit>
<subTitle>1</subTitle>
<details>d1</details>
<subTitle>2</subTitle>
<details>d2</details>
<subTitle>3</subTitle>
<details>d3</details>
<subTitle>4</subTitle>
<details>d4</details>
<subTitle>5</subTitle>
<details>d5</details>
<subTitle>6</subTitle>
<details>d6</details>
<subTitle>7</subTitle>
<details>d7</details>
<subTitle>8</subTitle>
<details>d8</details>
<subTitle>9</subTitle>
<details>d9</details>
<subTitle>10</subTitle>
<details>d10</details>
</Top10PlacestoVisit>
</Content>
</Destination>
</ListOfDestinations>
,所以我希望得到所有subTitle
和details
到list.how我能做到這一點。這是我試過的。
XmlDocument DestinationXml = new XmlDocument();
DestinationXml.Load(Server.MapPath("~/Xml_Data/Destination.xml"));
var xdoc = XDocument.Parse(DestinationXml.InnerXml);
var selectedCity = xdoc.Descendants("ListOfDestinations")
.Select(d => d.Elements("Destination")
.Where(w => w.Element("City").Value == DesName));
在這個地方得到<Top10PlacestoVisit></Top10PlacestoVisit>
裏面的所有數據(字幕和細節)然後我創建了一個Model
和賦值像this.but我不能讓subTitle
和details
一次到list.this是我在這裏獲得成功,但subTitle
我怎樣才能得到試圖
foreach (var itemz in selectedCity)
{
var needed = itemz.Elements("Top10PlacestoVisit");
foreach (var nitems in needed)
{
var getSubs = needed.Elements("details");
foreach (var itm in getSubs)
{
details.Add(new Destinations
{
details = itm.Value
});
}
}
}
ViewBag.details = details;
210並在同一時間分配它。幫助我這一點。
nice work.its working.thank you very – bill