C#从XML文档读取
示例
一个示例XML文件
<Sample> <Account> <One number="12"/> <Two number="14"/> </Account> <Account> <One number="14"/> <Two number="16"/> </Account> </Sample>
从此XML文件读取:
using System.Xml; using System.Collections.Generic; public static void Main(string fullpath) { var xmldoc = new XmlDocument(); xmldoc.Load(fullpath); var oneValues = new List<string>(); //使用标签名称获取所有XML节点 var accountNodes = xmldoc.GetElementsByTagName("Account"); for (var i = 0; i < accountNodes.Count; i++) { //使用Xpath查找节点 var account = accountNodes[i].SelectSingleNode("./One"); if (account != null &&account.Attributes!= null) { //读取节点属性 oneValues.Add(account.Attributes["number"].Value); } } }