如何使用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