1.读取xml文件
using System.Xml;
1-1:
XmlDocument xml = new XmlDocument();
xml.Load("XMLFile.xml");
string deviceIP = xml.SelectSingleNode("para/deviceIP").InnerText;
string devicePort = xml.SelectSingleNode("para/devicePort").InnerText;
string localIP = xml.SelectSingleNode("para/localIP").InnerText;
string localPort = xml.SelectSingleNode("para/localPort").InnerText;
1-2:
public class BDataStruct
{
public string mode;
ublic string ratioW;
public string ratioH;
public string[] filePath = new string[6];
public string[] delaytime = new string[6];
}<pre name="code" class="csharp"> if (File.Exists("ProgramConfig.xml"))
{
List<BDataStruct> m_PageDatas;
m_PageDatas = new List<BDataStruct>();
XmlDocument xml = new XmlDocument();
xml.Load("ProgramConfig.xml");
XmlElement rootElem = xml.DocumentElement; //获取根节点
XmlNodeList childNodes = rootElem.GetElementsByTagName("Para"); //获取Para子节点集合
//遍历读取xml的数据
foreach (XmlNode node in childNodes)
{
BDataStruct data = new BDataStruct();
data.mode = ((XmlElement)node).GetElementsByTagName("mode")[0].InnerText;
data.ratioW = ((XmlElement)node).GetElementsByTagName("ratioW")[0].InnerText;
data.ratioH = ((XmlElement)node).GetElementsByTagName("ratioH")[0].InnerText;
for (int j = 0; j < 6; j++)
{
string num = (j + 1).ToString();
data.filePath[j] = ((XmlElement)node).GetElementsByTagName("path" + num)[0].InnerText;
data.delaytime[j] = ((XmlElement)node).GetElementsByTagName("delay" + num)[0].InnerText;
}
m_PageDatas.Add(data);
}
}
{
public string mode;
ublic string ratioW;
public string ratioH;
public string[] filePath = new string[6];
public string[] delaytime = new string[6];
}<pre name="code" class="csharp"> if (File.Exists("ProgramConfig.xml"))
{
List<BDataStruct> m_PageDatas;
m_PageDatas = new List<BDataStruct>();
XmlDocument xml = new XmlDocument();
xml.Load("ProgramConfig.xml");
XmlElement rootElem = xml.DocumentElement; //获取根节点
XmlNodeList childNodes = rootElem.GetElementsByTagName("Para"); //获取Para子节点集合
//遍历读取xml的数据
foreach (XmlNode node in childNodes)
{
BDataStruct data = new BDataStruct();
data.mode = ((XmlElement)node).GetElementsByTagName("mode")[0].InnerText;
data.ratioW = ((XmlElement)node).GetElementsByTagName("ratioW")[0].InnerText;
data.ratioH = ((XmlElement)node).GetElementsByTagName("ratioH")[0].InnerText;
for (int j = 0; j < 6; j++)
{
string num = (j + 1).ToString();
data.filePath[j] = ((XmlElement)node).GetElementsByTagName("path" + num)[0].InnerText;
data.delaytime[j] = ((XmlElement)node).GetElementsByTagName("delay" + num)[0].InnerText;
}
m_PageDatas.Add(data);
}
}
==================================================================================================
2.生成xml文件
XmlDocument xmldoc = new XmlDocument();
XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmldoc.AppendChild(xmlnode);
XmlElement root = xmldoc.CreateElement("", "root", ""); //加入一个根元素
xmldoc.AppendChild(root);
XmlElement eb = xmldoc.CreateElement("mode");
eb.InnerText = mode;(从外部获取)
XmlElement ec = xmldoc.CreateElement("ratioW");
ec.InnerText = ratioW;(从外部获取)
XmlElement ed = xmldoc.CreateElement("ratioH");
ed.InnerText = ratioH;(从外部获取)
XmlElement ee = xmldoc.CreateElement("program");
XmlElement ef = xmldoc.CreateElement("time");
XmlElement ee1 = xmldoc.CreateElement("program1");
ee1.InnerText = exeString1;(从外部获取)
XmlElement ef1 = xmldoc.CreateElement("time1");
ef1.InnerText = counter1;(从外部获取)
XmlElement ee2 = xmldoc.CreateElement("program2");
ee2.InnerText = exeString2;(从外部获取)
XmlElement ef2 = xmldoc.CreateElement("time2");
ef2.InnerText = counter2;(从外部获取)
XmlElement ee3 = xmldoc.CreateElement("program3");
ee3.InnerText = exeString3;(从外部获取)
XmlElement ef3 = xmldoc.CreateElement("time3");
ef3.InnerText = counter3;(从外部获取)
XmlElement ee4 = xmldoc.CreateElement("program4");
ee4.InnerText = exeString4;(从外部获取)
XmlElement ef4 = xmldoc.CreateElement("time4");
ef4.InnerText = counter4;(从外部获取)
root.AppendChild(eb);
root.AppendChild(ec);
root.AppendChild(ed);
ee.AppendChild(ee1);
ef.AppendChild(ef1);
ee.AppendChild(ee2);
ef.AppendChild(ef2);
ee.AppendChild(ee3);
ef.AppendChild(ef3);
ee.AppendChild(ee4);
ef.AppendChild(ef4);
root.AppendChild(ee);
root.AppendChild(ef);
if (MessageBox.Show("确定保存当前配置信息?", "信息提示",
MessageBoxButtons.OKCancel) == DialogResult.OK)
{
xmldoc.Save(@"ProgramConfig.xml");
}
生成xml文件如下:
