1、c++版
1.1解析
// 加载 BOOL CConsumStatic::LoadXmlFile(CString strXml) { USES_CONVERSION; TiXmlDocument doc(T2A(strXml)); if (!doc.LoadFile()) { ZEBRA_HANDLER_AND_RETURN(etInstrumentControl,eciUserDefine,elLog,"Cannot load statistic file."); }
TiXmlElement *root = doc.RootElement(); TiXmlElement *device; if (!root) { ZEBRA_HANDLER_AND_RETURN(etInstrumentControl,"Cannot get root statistic file."); } //加载device device = root->FirstChildElement(); while (device) { const char *value = device->Value(); if (strcmp(value,"Camera") == 0)// 相机 { if (device->Attribute("opennum1")) { m_consumStatic.camera[0].opennum = atoi(device->Attribute("opennum1")); } } device = device->NextSiblingElement(); }
return TRUE; }
1.2保存
// 保存 BOOL CConsumStatic::SaveXmlFile(CString strXml) { USES_CONVERSION; TiXmlDocument *xmlDoc = new TiXmlDocument();//建立文件句柄 TiXmlDeclaration *decl = new TiXmlDeclaration("1.0","utf-8",""); TiXmlElement *device = new TiXmlElement("Device");//建立一个成员 xmlDoc->LinkEndChild(decl); xmlDoc->LinkEndChild(device);
// 相机 TiXmlElement *camera = new TiXmlElement("Camera"); device->LinkEndChild(camera); camera->SetAttribute("opennum1",m_consumStatic.camera[0].opennum);
xmlDoc->SaveFile(T2A(strXml)); delete xmlDoc;
return TRUE; }
2、c#版
2.1解析
/// <summary> /// 打开 /// </summary> /// <param name="path"></param> /// <returns></returns> public ERRCODE_E Load(string path) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode rootNode = doc.SelectSingleNode("ReagentDefinitions"); if (rootNode == null) { return ERRCODE_E.ERR_OPENFILE; } m_reagentDic.Clear(); XmlElement root = (XmlElement)rootNode; version = root.GetAttribute("Version").ToString(); XmlNodeList nodeList = rootNode.ChildNodes; foreach (XmlNode node in nodeList) { XmlElement xe = (XmlElement)node; string name = xe.GetAttribute("Name").ToString(); if (m_reagentDic.ContainsKey(name)) { continue; } m_reagentDic.Add(name,new List<ReagentInfo>());
XmlNodeList reagentList = node.ChildNodes; foreach (XmlNode reagent in reagentList) { ReagentInfo info = new ReagentInfo(); if (reagent.Attributes["Name"] != null) { info.Name = reagent.Attributes["Name"].InnerText; } if (reagent.Attributes["Description"] != null) { info.Description = reagent.Attributes["Description"].InnerText; } if (reagent.Attributes["Position"] != null) { info.Position = reagent.Attributes["Position"].InnerText; }
m_reagentDic[name].Add(info); } } } catch (Exception ex) { Console.WriteLine(ex.Message); return ERRCODE_E.ERR_EXCEPTION; }
return ERRCODE_E.ERR_SUCCESS; }
2.2保存
/// <summary> ///保存 /// </summary> /// <returns></returns> private voidGetXmlDoc() { XmlDocument doc = new XmlDocument(); XmlDeclaration dec = doc.CreateXmlDeclaration("1.0",null); doc.AppendChild(dec);
//创建一个根节点(一级) XmlElement root = doc.CreateElement("ReagentDefinitions"); root.SetAttribute("Version",version); doc.AppendChild(root);
foreach (KeyValuePair<string,List<ReagentInfo>> namePair in m_reagentDic) { //创建节点(二级) XmlElement namexe = doc.CreateElement("ReagentDefinition"); namexe.SetAttribute("Name",namePair.Key); foreach (ReagentInfo info in namePair.Value) { //创建节点(三级) XmlElement infoxe = doc.CreateElement("Reagent"); infoxe.SetAttribute("Name",info.Name); infoxe.SetAttribute("Description",info.Description); infoxe.SetAttribute("Position",info.Position); namexe.AppendChild(infoxe); } root.AppendChild(namexe); }
doc.Save(path);; } (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|