我有几个XML文件,我希望从中读取属性.我的主要目标是将语法突出显示应用于富文本框.
例如,在我的一个XML文档中,我有:<Keyword name="using">[..]所有文件都具有相同的元素:Keyword.
那么,我如何获取属性的值name并将它们放在每个XML文件的字符串集合中.
我正在使用Visual C#2008.
其他答案可以完成这项工作 - 但突出显示的语法和你所说的几个 xml 文件让我认为你需要更快的东西,为什么不使用精益和平均XmlReader呢?
private string[] getNames(string fileName)
{
XmlReader xmlReader = XmlReader.Create(fileName);
List<string> names = new List<string>();
while (xmlReader.Read())
{
//keep reading until we see your element
if (xmlReader.Name.Equals("Keyword") && (xmlReader.NodeType == XmlNodeType.Element))
{
// get attribute from the Xml element here
string name = xmlReader.GetAttribute("name");
// --> now **add to collection** - or whatever
names.Add(name);
}
}
return names.ToArray();
}
Run Code Online (Sandbox Code Playgroud)
另一个不错的选择是XPathNavigator类 - 它比 XmlDoc 更快,并且您可以使用 XPath。
另外,我建议您在尝试使用对性能不满意的简单选项后,仅在IFF时才采用此方法。
| 归档时间: |
|
| 查看次数: |
17561 次 |
| 最近记录: |