NoiseScene.xml:
View Code
1 <pre name="code" class="html"><?xml version="1.0" encoding="utf-8"?>
2 <DataTable>
3 <Rows>
4 <Scene>默认</Scene>
5 <Noise>-100</Noise>
6 </Rows>
7 <Rows>
8 <Scene>高层</Scene>
9 <Noise>-80</Noise>
10 </Rows>
11 </DataTable>
*.cs:
View Code
1 private string xml_NoiseScene = AppDomain.CurrentDomain.BaseDirectory + "NoiseScene.xml";
2 /// <summary>
3 /// 加载噪声场景
4 /// </summary>
5 /// <returns></returns>
6 public DataTable LoadNoise()
7 {
8 if (!File.Exists(xml_NoiseScene)) return null;
9 XmlDocument docXml = new XmlDocument();
10 docXml.Load(xml_NoiseScene);
11 return GetDataTable(docXml.InnerXml);
12 }
13 /// <summary>
14 /// 保存噪声场景
15 /// </summary>
16 /// <returns></returns>
17 public void SaveNoise(DataTable dt)
18 {
19 if (!File.Exists(xml_NoiseScene)) return;
20 File.SetAttributes(xml_NoiseScene, FileAttributes.Normal); //修改文件只读为可写
21 XmlDocument docXml = new XmlDocument();
22 docXml.InnerXml = GetXml(dt);
23 docXml.Save(xml_NoiseScene);
24 }
25 /// <summary>
26 /// XML2DT
27 /// </summary>
28 /// <param name="xmlStr"></param>
29 /// <returns></returns>
30 private DataTable GetDataTable(string xmlStr)
31 {
32 XmlDocument doc = new XmlDocument();
33 doc.LoadXml(xmlStr);
34 XmlNodeList xlist = doc.SelectNodes("//DataTable/Rows");
35 DataTable Dt = new DataTable();
36 DataRow Dr;
37 for (int i = 0; i < xlist.Count; i++)
38 {
39 Dr = Dt.NewRow();
40 XmlElement xe = (XmlElement)xlist.Item(i);
41 for (int j = 0; j < xe.Attributes.Count; j++)
42 {
43 if (!Dt.Columns.Contains("@" + xe.Attributes[j].Name))
44 Dt.Columns.Add("@" + xe.Attributes[j].Name);
45 Dr["@" + xe.Attributes[j].Name] = xe.Attributes[j].Value;
46 }
47 for (int j = 0; j < xe.ChildNodes.Count; j++)
48 {
49 if (!Dt.Columns.Contains(xe.ChildNodes.Item(j).Name))
50 Dt.Columns.Add(xe.ChildNodes.Item(j).Name);
51 Dr[xe.ChildNodes.Item(j).Name] = xe.ChildNodes.Item(j).InnerText;
52 }
53 Dt.Rows.Add(Dr);
54 }
55 return Dt;
56 }
57 /// <summary>
58 /// DT2XML
59 /// </summary>
60 /// <param name="dt"></param>
61 /// <returns></returns>
62 private string GetXml(DataTable dt)
63 {
64 string strXml = @"<?xml version='1.0' encoding='utf-8'?><DataTable />";
65 XmlDocument doc = new XmlDocument();
66 doc.LoadXml(strXml);
67 XmlNode root = doc.SelectSingleNode("//DataTable");
68 // 创建子节点
69 for (int j = 0; j < dt.Rows.Count; j++)
70 {
71 XmlElement xe = doc.CreateElement("Rows");
72 XmlElement xeChild = null;
73 if (!Object.Equals(dt, null))
74 {
75 for (int i = 0; i < dt.Columns.Count; i++)
76 {
77 if (dt.Columns[i].ColumnName.StartsWith("@"))
78 {
79 string AttributeName = dt.Columns[i].ColumnName.Replace("@", "");
80 // 为该子节点设置属性
81 xe.SetAttribute(AttributeName, dt.Rows[j][i].ToString());
82 }
83 else
84 {
85 xeChild = doc.CreateElement(dt.Columns[i].ColumnName);
86 try
87 {
88 xeChild.InnerXml = dt.Rows[j][i].ToString();
89 }
90 catch
91 {
92 xeChild.InnerText = dt.Rows[j][i].ToString();
93 }
94 xe.AppendChild(xeChild);
95 }
96 }
97 }
98 // 保存子节点设置
99 root.AppendChild(xe);
100 }
101 return doc.InnerXml.ToString();
102 }