这可能是一个愚蠢的问题,但我想要问,如何从具有不同内容的.XML文件中保存相同的元素作为数组。 示例XML:
<ithem>
<description>description1</description>
<description>description2</description>
<description>description3</description>
</ithem>
然后string []描述将是
descriptions[0] = "description1";
descriptions[1] = "description2";
descriptions[2] = "description3";
请帮忙!
答案 0 :(得分:0)
使用LINQ to XML,它将是:
XElement root = XElement.Load(xmlFile);
string[] descriptions = root.Descendants("description").Select(e => e.Value).ToArray();
或
string[] descriptions = root.Element("ithem").Elements("description").Select(e => e.Value).ToArray();
答案 1 :(得分:0)
使用XmlDocument解析XML:
var map = new XmlDocument();
map.Load("path_to_xml_file"); // you can also load it directly from a string
var descriptions = new List<string>();
var nodes = map.DocumentElement.SelectNodes("ithem/description");
foreach (XmlNode node in nodes)
{
var description = Convert.ToString(node.Value);
descriptions.Add(description);
}
你可以从以下网址获得它:
descriptions.ToArray();