C# 遍历所有未知的XML属性
我想用C#遍历XML文件中所有给定标记的所有属性,如本例所示C# 遍历所有未知的XML属性,c#,xml,C#,Xml,我想用C#遍历XML文件中所有给定标记的所有属性,如本例所示 <TAG1 Attribute1="1234" Attribute2="1234" Attribute3="1234"> <TAG2 xpq="123" abc="000" lkj="123"/> </TAG1> 但我只知道标签名,其他什么都没有 有人对我有什么想法吗?我更喜欢使用 您应该使用该类,它提供了对XML文档更简单的访问 var doc = XDocument.Load("C
<TAG1 Attribute1="1234" Attribute2="1234" Attribute3="1234">
<TAG2 xpq="123" abc="000" lkj="123"/>
</TAG1>
但我只知道标签名,其他什么都没有
有人对我有什么想法吗?我更喜欢使用
您应该使用该类,它提供了对XML文档更简单的访问
var doc = XDocument.Load("C:\\PathToXml\File.xml");
foreach(var el in doc.Descendants())
foreach(var attr in el.Attributes())
{
var itemValue = new ListViewItem(new[] { el.TagName, attr.Name, attr.Value });
listView1.Items.Add(itemValue);
}
您应该使用
XDocument
加载xml文件。然后像这样使用XElement.attributes
获取所有属性
var document = XDocument.Load("C:\\PathToXml\File.xml");
foreach (var attribute in document.Root.Attributes)
{
//process the attribute
//attribute.Name
//attribute.Value
}
Duplicate显示XmlReader解决方案,或者如果您对LINQ to XML没有问题,则显示-Duplicate of。
var doc = XDocument.Load("C:\\PathToXml\File.xml");
foreach(var el in doc.Descendants())
foreach(var attr in el.Attributes())
{
var itemValue = new ListViewItem(new[] { el.TagName, attr.Name, attr.Value });
listView1.Items.Add(itemValue);
}
var document = XDocument.Load("C:\\PathToXml\File.xml");
foreach (var attribute in document.Root.Attributes)
{
//process the attribute
//attribute.Name
//attribute.Value
}