我想用XML文档中的元素填充一个新的字符串数组。
在VBA中,我用一个集合管理了这个逻辑:
For Each x In xmlDoc.SelectNodes("//a")
MyCollection.add (x.Attributes.getNamedItem("href").Text)
Next x
但我真正想做的是将相同的集合放入C#中的数组中,例如:
string[] MyArray = new string[]
{
xmlDoc.SelectNodes("//a").Attributes.getNamedItem("href").Text
};
这可能吗?或者以某种类似的方式做到这一点而不循环并单独添加到数组?
答案 0 :(得分:1)
这样做
string[] MyArray = XDocument.Parse(xml).XPathSelectElements("//a").Select(e => e.Attributes("href").FirstOrDefault().Value).ToArray()`
修改:需要Using System.Xml.XPath
和Using System.XML.Linq
才能运作