假设我有一个 XML 文件:
<locations>
<country name="Australia">
<city>Brisbane</city>
<city>Melbourne</city>
<city>Sydney</city>
</country>
<country name="England">
<city>Bristol</city>
<city>London</city>
</country>
<country name="America">
<city>New York</city>
<city>Washington</city>
</country>
</locations>
我希望它变平(这应该是最终结果):
Australia
Brisbane
Melbourne
Sydney
England
Bristol
London
America
New York
Washington
我试过这个:
var query = XDocument.Load(@"test.xml").Descendants("country")
.Select(s => new
{
Country = (string)s.Attribute("name"),
Cities = s.Elements("city")
.Select (x => new { City = (string)x })
});
但这会返回一个嵌套列表query
。像这样:
{ Australia, Cities { Brisbane, Melbourne, Sydney }},
{ England, Cities { Bristol, London }},
{ America, Cities { New York, Washington }}
谢谢