如何使用C#以正确的方式获取属性“action”和“filename”值?C#从xml属性获取值
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 (action in xmlActions)
{
//not working
}
foreach (file in xmlFileNames)
{
//not working
}
你的代码示例对我意味着很多。谢谢!
您可能想查看[LINQ to XML](http://msdn.microsoft.com/library/bb387087.aspx)。它使得使用XML更容易。 – Corak