日期:2014-05-18  浏览次数:21091 次

c#怎么读xml文件?

<users>
<user username="aaa" password="aaa"></user>
<user username="bbb" password="bbb"></user>
<user username="ccc" password="ccc"></user>
</users>

这个文件, 我想用程序读每一个 username 和 password

------解决方案--------------------
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\Project\C#Test\WindowsApplication1\WindowsApplication3\Config.xml");
XmlNode node = doc.SelectNodes("/users");

foreach (XmlNode n in node.ChildNodes)
{
string name= n.Attributes["username"].InnerText;
string pwd=n.Attributes["password"].InnerText;
}


------解决方案--------------------
先以xml方式读出配置节,然后遍历读出子节:
public static void saveAPPCONFIG(String skeyname,String skeyvalue)
{
try
{
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
xmldoc.Load("App.config");

foreach (System.Xml.XmlNode xnode in xmldoc["configuration"]["appSettings"])
{
MessageBox.Show(xnode.Attributes.GetNamedItem("value").Value.ToString();
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}