【发布时间】:2017-05-20 06:20:40
【问题描述】:
我想用 C# 遍历 XML 文件中所有给定标签的所有属性,就像在这个例子中一样。
<TAG1 Attribute1="1234" Attribute2="1234" Attribute3="1234">
<TAG2 xpq="123" abc="000" lkj="123"/>
</TAG1>
XML 很长,标签和属性命名可以更改。 所以我必须找到一种方法来用未知的命名迭代它们。 我已经用
测试过了XmlTextReader reader = new XmlTextReader("C:\\PathToXml\File.xml");
var optionTitle = reader.Name;
var AttributeName = reader.LocalName;
var AttributeValue = reader.GetAttribute(AttributeName);
var itemValue = new ListViewItem(new[] { optionTitle, AttributeName, AttributeValue });
listView1.Items.Add(itemValue);
但我只得到标签名称,没有别的......
有人对我有什么想法吗?
【问题讨论】:
-
Duplicate 显示 XmlReader 解决方案,或者如果您对 LINQ-to-XML 没问题 - stackoverflow.com/questions/6253565/… 的副本。