A:在XML文件中有多个集,每个集里面包含多个节点。
B:可根据集的名字的取出这个集合下面的所有节点值
C:可一次性取出所有的节点值
D:其中有一个是无限拓展的就是:表达式
E:把集合读取到一个模型(实体类)当中
getmxl.cs文件
代码如下 |
复制代码 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace XmlOperation
{
/*
* 使用方法:调用GetCommandByName("指令名") 获取该指令下的所有信息
* XML文件规范:见该本页代码底部
*
* 作者:Andrew
* BLOG:http://www.111com.net *
*/
class GetMxl
{
XmlNodeList nodeList;
public GetMxl(string xmlFilePath)
{
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
nodeList = doc.SelectNodes("/Equipment//Command");
}
///
/// 根据指令名获得该组指令的信息
///
/// 指令名
/// 返回该指令的所有信息
public List GetCommandByName(string commandName)
{
foreach (XmlNode xmlNode in nodeList)
{
if (xmlNode["name"].InnerText == commandName)
{
List cmdList = new List();
cmdList.Add(xmlNode["name"].InnerText);
cmdList.Add(xmlNode["value"].InnerText);
cmdList.Add(xmlNode["string"].InnerText);
for (int i = 3; i < xmlNode.ChildNodes.Count; i++)
{
cmdList.Add(xmlNode.ChildNodes[i].InnerText);
}
return cmdList;
}
}
return null;
}
///
/// 获得所有的指令名
///
public string[] Names
{
get
{
string[] names = new string[Count];
for (int i = 0; i < nodeList.Count; i++)
{
names[i] = nodeList[i]["name"].InnerText;
}
return names;
}
}
///
/// 获取一共有多少条指令
///
public int Count
{
get
{
return nodeList.Count;
}
}
}
}
/*
*
指令名2
指令值
正确的字符串
表达式1
表达式2
表达式3
表达式4
指令名
指令值
正确的字符串
表达式1
表达式2
表达式3
表达式4
表达式5
*
* 以上是XML文件规范写入
*/
|
xmcommandMode.cs文件
代码如下 |
复制代码 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XmlOperation
{
public class XmlCommandModel
{
public XmlCommandModel()
{
}
///
/// 设定XML文件路径,并且根据【指令名】获取一个指令集合对象
///
/// xml文件路径
/// 指令名
public XmlCommandModel(string xmlFilePath,string commandByName)
{
GetMxl getXml = new GetMxl(xmlFilePath);
List cmdList = getXml.GetCommandByName(commandByName);
commandName = cmdList[0];
commandValue = cmdList[1];
commandString = cmdList[2];
commandPuts = new string[cmdList.Count - 3];
for (int i = 3; i < cmdList.Count; i++)
{
commandPuts[i - 3] = cmdList[i];
}
}
private string commandName;
///
/// 指令名称
///
public string CommandName
{
get { return commandName; }
set { commandName = value; }
}
private string commandValue;
///
/// 指令值
///
public string CommandValue
{
get { return commandValue; }
set { commandValue = value; }
}
private string commandString;
///
/// 正确的字符串
///
public string CommandString
{
get { return commandString; }
set { commandString = value; }
}
private string[] commandPuts;
///
/// 表达式
///
public string[] CommandPuts
{
get { return commandPuts; }
set { commandPuts = value; }
}
}
}
|
xmlopt.cs文件
代码如下 |
复制代码 |
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace XmlOperation
{
public class XmlOpt
{
GetMxl getXml;
///
/// 实例化一个指定路径的实例
///
/// XML文件路径
public XmlOpt(string filePath)
{
this.filePath = filePath;
getXml = new GetMxl(filePath);
}
///
/// 根据指令名获取一个对象
///
/// 指令名
///
public XmlCommandModel GetCommandModelByName(string SelectCommandNmae)
{
XmlCommandModel cmd = new XmlCommandModel(filePath, SelectCommandNmae);
return cmd;
}
///
/// 获取所有指令的对象集合
///
public List GetCommandModelList
{
get
{
List cmdList = new List();
for (int i = 0; i < commandCout; i++)
{
XmlCommandModel cmdModel = new XmlCommandModel(filePath, CommandNames[i]);
cmdList.Add(cmdModel);
}
return cmdList;
}
}
///
/// 获取所有的指令名
///
public string[] CommandNames
{
get
{
return getXml.Names;
}
}
///
/// 获取一共多少条指令
///
public int commandCout
{
get
{
return getXml.Count;
}
}
private string filePath;
///
/// 获取已设置的文件路径
///
public string FilePath
{
get { return filePath; }
}
}
}
|
最后一个简单的配置codetest.snl
代码如下 |
复制代码 |
Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MyXML", "MyXMLMyXML.csproj", "{283858AE-82B4-4411-810F-C6AD19833132}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "XmlOperation", "XmlOperationXmlOperation.csproj", "{9A50C0DB-77F2-4B31-A451-90222CB56E00}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{283858AE-82B4-4411-810F-C6AD19833132}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{283858AE-82B4-4411-810F-C6AD19833132}.Debug|Any CPU.Build.0 = Debug|Any CPU
{283858AE-82B4-4411-810F-C6AD19833132}.Release|Any CPU.ActiveCfg = Release|Any CPU
{283858AE-82B4-4411-810F-C6AD19833132}.Release|Any CPU.Build.0 = Release|Any CPU
{9A50C0DB-77F2-4B31-A451-90222CB56E00}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9A50C0DB-77F2-4B31-A451-90222CB56E00}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9A50C0DB-77F2-4B31-A451-90222CB56E00}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9A50C0DB-77F2-4B31-A451-90222CB56E00}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal |