1、Xml 的页面:
2、建立或写入
在这之前,需要知道:从结构上讲.XmlElement(元素)是 XmlNode (节点) 的派生类 XmlNode 是 DOM 的.NET 实现中的基类。 在调用本函数前,需要 传入 结构(如:存储这内容的list链表),路径。
代码:
public void WriteTestRecordXml(TestRecordStruct TestRecord,string filePath) // 写入Xml 文件
{
string _xmlPath = filePath;
try
{
XmlDocument xmlDoc = new XmlDocument(); // 新建Xml文件
XmlElement xeData = null;
if (File.Exists(_xmlPath)) //判断xml文件是否存在,不存在-创建
{
xmlDoc.Load(_xmlPath); // 加载文件
XmlNode xnDatas = xmlDoc.SelectSingleNode("data");
xnDatas.RemoveAll(); // 去除date节点下的所有字节点,
xeData = xnDatas as XmlElement; // XnDatas节点 转换为一个元素。
}
else
{
XmlDeclaration xmldecl = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); // 在新建的Xml文件创建指定值得节点 <?xml version="1.0" encoding="UTF-8"?>
xmlDoc.AppendChild(xmldecl); // 然后加入到Xml文件中去
xeData = xmlDoc.CreateElement("", "data", ""); // 创建元素 元素为节点的派生类
xmlDoc.AppendChild(xeData); //添加data标签
}
//创建 TestRecords 标签
XmlElement xeTestRecords = xmlDoc.CreateElement("TestRecords"); //创建元素便签
for (int i = 0; i < TestRecord.TestRecords.Count; i++)
{
RecordStruct record = TestRecord.TestRecords[i];
//创建 record 标签
XmlElement xeRecord = xmlDoc.CreateElement("record");
xeRecord.SetAttribute("key", record.key); // 给这个元素标签赋予属性 和 属性值
xeRecord.SetAttribute("barcode", record.barcode);
xeRecord.SetAttribute("qrcode", record.qrcode);
xeRecord.SetAttribute("type", record.type);
xeRecord.SetAttribute("version", record.version);
xeRecord.SetAttribute("tester", record.tester);
xeRecord.SetAttribute("datetime", record.dateTime);
xeRecord.SetAttribute("TestResult", record.testResult);
xeRecord.SetAttribute("TestResultFile", record.testResultFile);
xeTestRecords.AppendChild(xeRecord); //在TestRecords添加record标签
}
xeData.AppendChild(xeTestRecords); //添加TestRecords标签
xmlDoc.Save(_xmlPath); //保存xml文件
}
catch (Exception ex)
{
LogManager.Instance.WriteErrLog("XmlManager", "WriteTestRecordXml", ex.ToString());
}
}
3、在某个节点在追加节点
public bool AddTestRecordXml(RecordStruct record) // 追加节点信息
{
string _xmlPath = _xmlConfigPath + "\\" + _xmlTestRecord;
try
{
//加载xml文件
XmlDocument doc = new XmlDocument();
doc.Load(_xmlPath);
//data/TestRecords标签
XmlNode xnRecords = doc.SelectSingleNode("data/TestRecords"); // TestRecords 选中节点
//创建 record 标签
XmlElement xeRecord = doc.CreateElement("record");
xeRecord.SetAttribute("key", record.key);
xeRecord.SetAttribute("barcode", record.barcode);
xeRecord.SetAttribute("qrcode", record.qrcode);
xeRecord.SetAttribute("type", record.type);
xeRecord.SetAttribute("version", record.version);
xeRecord.SetAttribute("tester", record.tester);
xeRecord.SetAttribute("datetime", record.dateTime);
xeRecord.SetAttribute("TestResult", record.testResult);
xeRecord.SetAttribute("TestResultFile", record.testResultFile);
xnRecords.AppendChild(xeRecord); //添加record标签
doc.Save(_xmlPath);//保存记录
return true;
}
catch (Exception ex)
{
LogManager.Instance.WriteErrLog("XmlManager", "AddTestRecordXml", ex.ToString());
return false;
}
}
4、读取节点 ,调用本函数前,需要传入 结构(存储从Xml中读取出来的内容)
public void ReadTestRecordXml(TestRecordStruct TestRecord)
{
string _xmlPath = _xmlConfigPath + "\\" + _xmlTestRecord;
try
{
TestRecord.TestRecords.Clear();
//加载xml文件
XmlDocument doc = new XmlDocument();
doc.Load(_xmlPath);
//data标签
XmlNode xnDatas = doc.SelectSingleNode("data"); // 选中文本中的第一层字节点xnDatas
//cmds标签
XmlNode xnRecords = xnDatas.SelectSingleNode("TestRecords"); // 选中子节点的字节点xnRecords
foreach (XmlNode xnRecord in xnRecords.ChildNodes) // 然后遍历 字节点xnRecords中的 字节点
{
XmlElement xeRecord = xnRecord as XmlElement; // 把这个节点转化为一个元素。
RecordStruct record = new RecordStruct();
record.key = xeRecord.GetAttribute("key"); // 根据节点名称 获取节点元素中的属性值
record.barcode = xeRecord.GetAttribute("barcode");
record.qrcode = xeRecord.GetAttribute("qrcode");
record.type = xeRecord.GetAttribute("type");
record.version = xeRecord.GetAttribute("version");
record.tester = xeRecord.GetAttribute("tester");
record.dateTime = xeRecord.GetAttribute("datetime");
record.testResult = xeRecord.GetAttribute("TestResult");
record.testResultFile = xeRecord.GetAttribute("TestResultFile");
TestRecord.TestRecords.Add(record); // 添加到链表中
}
}
catch (Exception ex)
{
LogManager.Instance.WriteErrLog("XmlManager", "ReadTestRecordXml", ex.ToString());
}
}
5、从 Xml中 根据某些键值,选取其中的一些节点,其中用到了正则表达式 和 Xml节点链表(XmlNodeList)
public TestRecordStruct SelectTestRecordByQrcodeXml(string qrcode)
{
TestRecordStruct TestRecord = new TestRecordStruct();
string _xmlPath = _xmlConfigPath + "\\" + _xmlTestRecord;
try
{
TestRecord.TestRecords.Clear();
//加载xml文件
XmlDocument doc = new XmlDocument();
doc.Load(_xmlPath);
//data标签
XmlNode xnDatas = doc.SelectSingleNode("data");
//TestRecords标签
XmlNode xnRecordP = xnDatas.SelectSingleNode("TestRecords");
// XmlNodeList 链表 后面是正则表达式
XmlNodeList xnRecords = xnRecordP.SelectNodes("record[@qrcode='" + qrcode + "']");
foreach (XmlNode xnRecord in xnRecords)
{
XmlElement xeRecord = xnRecord as XmlElement;
RecordStruct record = new RecordStruct();
record.key = xeRecord.GetAttribute("key");
record.barcode = xeRecord.GetAttribute("barcode");
record.qrcode = xeRecord.GetAttribute("qrcode");
record.type = xeRecord.GetAttribute("type");
record.version = xeRecord.GetAttribute("version");
record.tester = xeRecord.GetAttribute("tester");
record.dateTime = xeRecord.GetAttribute("datetime");
record.testResult = xeRecord.GetAttribute("TestResult");
record.testResultFile = xeRecord.GetAttribute("TestResultFile");
TestRecord.TestRecords.Add(record);
}
}
catch (Exception ex)
{
LogManager.Instance.WriteErrLog("XmlManager", "ReadTestRecordXml", ex.ToString());
}
return TestRecord;
}