Xml 文件读取

.NET 读取Xml文件,用到XmlDocument类。

1、要获取文档的根: DocumentElement

2、Attributes :获取 XmlAttributeCollection 包含此节点的属性。 (主体操作都是获取属性值)

 1  DirectoryInfo dir = new DirectoryInfo(@"D:\XXX\XML"); //文件夹路径
 2             FileInfo[] fileInfo = dir.GetFiles();
 3             Dictionary<string, List<string>> tabDic = new Dictionary<string, List<string>>();
 4             foreach (FileInfo item in fileInfo)
 5             {
 6                 string path = item.DirectoryName + "\\" + item.Name;
 7                 XmlDocument doc = new XmlDocument();
 8                 doc.Load(path);
 9                 XmlNode root = doc.DocumentElement; //根节点
10                 XmlNodeList nodes = root.SelectNodes("ormTable");
11                 foreach (XmlNode node in nodes)
12                 {
13                     string tableName = node.Attributes["tableName"].Value;  //获取表名
14                     List<string> lstField = new List<string>();     //文件字段长度>0的字段列表
15                     XmlNodeList fieldsNode = node.SelectNodes("Field"); //各个字段
16                     foreach (XmlNode fnode in fieldsNode)
17                     {
18                         //文件字段长度大于0,加入列表
19                         if (fnode.Attributes["filePath"].Value.Length > 0)
20                         {
21                             lstField.Add(fnode.Attributes["fieldName"].Value);
22                         }
23                     }
24                     if (lstField.Count > 0)
25                     {
26                         //表名不在字典中
27                         if (!tabDic.ContainsKey(tableName))
28                         {
29                             tabDic.Add(tableName, lstField);
30                         }
31                     }
32                 }
33             }
读取文件夹中所有Xml文件

猜你喜欢

转载自www.cnblogs.com/meng9527/p/9083331.html