读取txt和xml等,生成Dictionary等

TXT文件读取

pac文件格式:
11000,XX省
11100,XX市
11101,XXXX区
11102,XXXX1区

1
2
3
4
5
6
7
8
9
10
11
12
13
public static Dictionary<string, string> countyPacMap = new Dictionary<string, string>();//存储区域PAC码对应信息
FileStream file = new FileStream("Config/PAC.txt", FileMode.OpenOrCreate);
StreamReader reader = new StreamReader(file, UnicodeEncoding.GetEncoding("UTF-8"));
string strLine = string.Empty;
while ((strLine = reader.ReadLine()) != null)
{
strLine = strLine.Trim().ToString();
string[] nameAndCode = strLine.Split(',');
string name = nameAndCode[1];
string pac = nameAndCode[0];
countyPacMap.Add(name, pac);
}
file.Close();

XML读取

XML格式:

1
2
3
4
5
6
7
<Directory Name="XX" >
<Directory Name="1-采样数据" >
<File DestinationDirectory="1-采样数据" isCheckAttribute="true" type="1" sheetNum="1" >XXX.txt</File>
<File DestinationDirectory="1-采样数据">2-登记表.pdf</File>
<File DestinationDirectory="1-采样数据">3-送样单.pdf</File>
</Directory>
</Directory>

读取XML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 public static List<FieldModel> getFieldsFromXml(String xmlName, int index)
{
string m_strConfig = "Tables/";
List<FieldModel> result = new List<FieldModel>();
XmlDocument doc = new XmlDocument();
doc.Load(m_strConfig + xmlName + ".xml");
XmlNode root = doc.SelectSingleNode("Table");
XmlNode sheet = root.ChildNodes[index];
// 得到根节点的所有子节点
XmlNodeList xnl = sheet.ChildNodes;
foreach (XmlNode xn in xnl)
{
FieldModel fieldModel = new FieldModel();
// 将节点转换为元素,便于得到节点的属性值
XmlElement xe = (XmlElement)xn;
fieldModel.FieldName = xe.GetAttribute("Name").ToString();
fieldModel.FieldType = xe.GetAttribute("Type").ToString();
fieldModel.RuleType = xe.GetAttribute("RuleType").ToString();
fieldModel.RuleParms = xe.GetAttribute("RuleParms").ToString();
result.Add(fieldModel);
}
return result;
}
public static string[] getFieldCNames(ArrayList array)
{
int nums = array.Count;
string[] names = new string[nums];
for (int i = 0; i < nums; i++)
{
FieldModel fieldModel = (FieldModel)array[i];
names[i] = fieldModel.FieldName;
}
return names;
}
//FieldModel类
class FieldModel
{
public FieldModel()
{

}
private string fieldName;

public string FieldName
{
get { return fieldName; }
set { fieldName = value; }
}

private string ruleType;

public string RuleType
{
get { return ruleType; }
set { ruleType = value; }
}
private string fieldType;

public string FieldType
{
get { return fieldType; }
set { fieldType = value; }
}
private string ruleParms;

public string RuleParms
{
get { return ruleParms; }
set { ruleParms = value; }
}




}