用C#对已有的xml文件进行解析,并将获得的数据按照自己的需求进行保存方便右后调用
以如下的xml文件为例
笔者的xml文件后续还有很多节点信息,出于保密这里只是部分截取
对于如上的xml文件,这里采用XmlDocument的方式,共有三种方式,可自行百度。
首先创建一个类用来保存读取的信息
在保存信息时,可以采取多种方式,数组,list容器,结构体
class Project
{
public string projectName = “empty”;
public string modelFormat = “empty”;
public string modelPath = “empty”;
public string worker = “empty”;
public string date = “empty”;
public string simulationType = “Structural”;
public string name = “empty”;
public struct Struct_Post
{
public string struct_name;public string postId;public string result_type;public void reset(){struct_name = \"Post Deal Infomation\";postId = \"No paras\";result_type = \"No paras\";//geoInfo.reset();}};public struct Struct_NameSels{public string nameSel_name;public void reset(){nameSel_name = \"No paras\";}};public Struct_Post post_struct;//保存后处理信息public List<Struct_NameSels> NameSelectionInfos;}
在依据需求创建好类后开始读取xml文件并保存
class Prepare:Project
{
//public List projectList = new List();
public override bool Initial(string xmlPath)
{
string str1 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
XmlDocument Document = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreComments = true;//忽略文档里面的注释
try{//xml文件路径XmlReader reader = XmlReader.Create(xmlPath, settings);Document.Load(reader);}catch (System.Exception ex){//MessageBox.Show(\"读取文件失败!\");MessageBox.Show(ex.Message);}//得到根节点ProjectXmlNode XnPj = Document.SelectSingleNode(\"Project\");//得到根节点的所有子节点if (XnPj != null){XmlElement pj = (XmlElement)XnPj;//获得根节点的属性值projectName = pj.GetAttribute(\"prjName\").ToString();worker = pj.GetAttribute(\"worker\").ToString();date = pj.GetAttribute(\"date\").ToString();simulationType = pj.GetAttribute(\"simulationType\").ToString();//projectList.Add(projectInfomation);//得到project 的所有子节点XmlNodeList XnPjChild = XnPj.ChildNodes;for (int a = 0; a < XnPjChild.Count; a++){string pointname1 = XnPjChild.Item(a).Name;if (pointname1 == \"Workbench\"){//得到workbench的所有子节点XmlNodeList Xnwb = XnPjChild.Item(a).ChildNodes;for (int b = 0; b < Xnwb.Count; b++){string pointname2 = Xnwb.Item(b).Name;if (pointname2 == \"Model\"){//获得model的属性name = ((XmlElement)Xnwb.Item(b)).GetAttribute(\"name\").ToString();//得到model的所有子节点XmlNodeList XnMd = Xnwb.Item(b).ChildNodes;for (int c = 0; c < XnMd.Count; c++){string pointname3 = XnMd.Item(c).Name;if (pointname3 == \"ModelFormat\"){//获得modelFormat的属性modelFormat = XnMd.Item(c).InnerText;}if (pointname3 == \"ModelPath\"){//获得modelPath的属性modelPath = XnMd.Item(c).InnerText;}}}if (pointname2 == \"Material\"){XmlNodeList XnMatr = Xnwb.Item(b).ChildNodes;for (int c = 0; c < XnMatr.Count; c++){string pointname3 = XnMatr.Item(c).Name;if (pointname3 == \"MaterailXmlPath\"){//获得MaterailXmlPath的属性值materialInfoPath = XnMatr.Item(c).InnerText;}}}}}{//后续内容已省略..........}}
总结
对于解析xml文件,有如下总结,
首先确定读取的信息,声明变量用以保存,其次在读取xml文件时要很清楚的搞清楚根节点和子节点(相对而言),按行读取,遇到根节点,则获得根节点的子节点, 例如: XmlNode XnPj = Document.SelectSingleNode(“Project”);若根节点包含属性值或者内容则按不同的方式获取,
获取属性值:XmlElement pj = (XmlElement)XnPj;
//获得根节点的属性值
projectName = pj.GetAttribute(“prjName”).ToString();
//获得modelFormat的内容
modelFormat = XnMd.Item©.InnerText;
最后并且遍历子节点,若子节点又包含子节点,则重复之前步骤…依次按行读取直到最终完成。