xml解析与保存

前端之家收集整理的这篇文章主要介绍了xml解析与保存前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_502_0@1、c++版

@H_502_0@1.1解析

@H_502_0@// 加载
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.");
}

@H_502_0@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();
}

@H_502_0@return TRUE;
}

@H_502_0@1.2保存

@H_502_0@
// 保存
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);

@H_502_0@// 相机
TiXmlElement *camera = new TiXmlElement("Camera");
device->LinkEndChild(camera);
camera->SetAttribute("opennum1",m_consumStatic.camera[0].opennum);

@H_502_0@xmlDoc->SaveFile(T2A(strXml));
delete xmlDoc;

@H_502_0@return TRUE;
}

@H_502_0@2、c#版

@H_502_0@2.1解析

@H_502_0@/// <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>());

@H_502_0@ 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;
}

@H_502_0@ m_reagentDic[name].Add(info);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return ERRCODE_E.ERR_EXCEPTION;
}

@H_502_0@ return ERRCODE_E.ERR_SUCCESS;
}

@H_502_0@2.2保存

@H_502_0@/// <summary>
///保存
/// </summary>
/// <returns></returns>
private voidGetXmlDoc()
{
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0",null);
doc.AppendChild(dec);

@H_502_0@ //创建一个根节点(一级)
XmlElement root = doc.CreateElement("ReagentDefinitions");
root.SetAttribute("Version",version);
doc.AppendChild(root);

@H_502_0@ 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);
}

@H_502_0@ doc.Save(path);; }

猜你在找的XML相关文章