Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 遍历所有未知的XML属性_C#_Xml - Fatal编程技术网

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

我想用C#遍历XML文件中所有给定标记的所有属性,如本例所示

<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
}