如何使用 C# 以正确的方式获取属性“动作”和“文件名”值?
XML:
<?xml version="1.0" encoding="utf-8" ?>
<Config version="1.0.1.1" >
<Items>
<Item action="Create" filename="newtest.xml"/>
<Item action="Update" filename="oldtest.xml"/>
</Items>
</Config>
C#:我无法获取属性值以及如何在 foreach 循环中获取值?如何解决这个问题?
var doc = new XmlDocument();
doc.Load(@newFile);
var element = ((XmlElement)doc.GetElementsByTagName("Config/Items/Item")[0]); //null
var xmlActions = element.GetAttribute("action"); //cannot get values
var xmlFileNames= element.GetAttribute("filename"); //cannot get values
foreach (var action in xmlActions)
{
//not working
}
foreach (var file in xmlFileNames)
{
//not working
}
您的代码示例对我来说意义重大。谢谢!