<Module Name="CPULIST01" Type="CPULIST" Version="1.1.0.0" Description="UNIT1.2">
<Connector Name="IF2" NodeNumber="4">
<Parameter ID="ActivateDevice" Value="1" />
<Parameter ID="EthInterfaceRedundant" Value="0" />
<Parameter ID="HostName" Value="if2-br-automation" />
<Parameter ID="Mode" Value="Manual" />
<Parameter ID="InternetAddress" Value="192.168.10.49" />
<Parameter ID="SubnetMask" Value="255.255.255.0" />
<Parameter ID="ActivateModbus" Value="1" />
<Parameter ID="NumSlaves" Value="-1" />
<Parameter ID="NumAllBlocks" Value="-1" />
<Parameter ID="MemSizeIn" Value="-1" />
<Parameter ID="MemSizeOut" Value="-1" />
</Connector>
<Connector Name="IF3">
<Parameter ID="Operation" Value="EthOnly" />
<Parameter ID="Baudrate" Value="100" />
<Parameter ID="EplEthInterfaceRedundant" Value="0" />
<Parameter ID="EthernetMode" Value="Manual" />
<Parameter ID="HostName" Value="if3-eth-br-automation" />
</Connector>
<Parameter ID="ConfigurationID" Value="Sx_Startup_NONRED_Unit1" />
<Parameter ID="Simulation" Value="1" />
<Parameter ID="VolatileGlobalPvSize" Value="75000" />
<Parameter ID="TimerDeviceType" Value="EPLX2X" />
<Parameter ID="TimerDevice" Value="X20IF2181-2.IF1" />
</Module>
现在我想搜索“模拟”是否存在,我该怎么做。 请建议。
答案 0 :(得分:0)
试试这个..
var doc_xml = new XmlDocument();
//load your xmlfile
doc_xml.Load(@"..\Yourxmlfile.xml");
foreach (XmlNode node in doc_xml.DocumentElement.ChildNodes)
{
//your code
}
答案 1 :(得分:0)
使用XPath是一种方式:
var xDoc = XDocument.Load("test.xml");
var element = xDoc.XPathSelectElement("Module/Parameter[@ID='Simulation']");
if (element != null)
{
// Simulation is present
}
答案 2 :(得分:0)
使用XML Linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string xml =
"<Module Name=\"CPULIST01\" Type=\"CPULIST\" Version=\"1.1.0.0\" Description=\"UNIT1.2\">" +
"<Connector Name=\"IF2\" NodeNumber=\"4\">" +
"<Parameter ID=\"ActivateDevice\" Value=\"1\" />" +
"<Parameter ID=\"EthInterfaceRedundant\" Value=\"0\" />" +
"<Parameter ID=\"HostName\" Value=\"if2-br-automation\" />" +
"<Parameter ID=\"Mode\" Value=\"Manual\" />" +
"<Parameter ID=\"InternetAddress\" Value=\"192.168.10.49\" />" +
"<Parameter ID=\"SubnetMask\" Value=\"255.255.255.0\" />" +
"<Parameter ID=\"ActivateModbus\" Value=\"1\" />" +
"<Parameter ID=\"NumSlaves\" Value=\"-1\" />" +
"<Parameter ID=\"NumAllBlocks\" Value=\"-1\" />" +
"<Parameter ID=\"MemSizeIn\" Value=\"-1\" />" +
"<Parameter ID=\"MemSizeOut\" Value=\"-1\" />" +
"</Connector>" +
"<Connector Name=\"IF3\">" +
"<Parameter ID=\"Operation\" Value=\"EthOnly\" />" +
"<Parameter ID=\"Baudrate\" Value=\"100\" />" +
"<Parameter ID=\"EplEthInterfaceRedundant\" Value=\"0\" />" +
"<Parameter ID=\"EthernetMode\" Value=\"Manual\" />" +
"<Parameter ID=\"HostName\" Value=\"if3-eth-br-automation\" />" +
"</Connector>" +
"<Parameter ID=\"ConfigurationID\" Value=\"Sx_Startup_NONRED_Unit1\" />" +
"<Parameter ID=\"Simulation\" Value=\"1\" />" +
"<Parameter ID=\"VolatileGlobalPvSize\" Value=\"75000\" />" +
"<Parameter ID=\"TimerDeviceType\" Value=\"EPLX2X\" />" +
"<Parameter ID=\"TimerDevice\" Value=\"X20IF2181-2.IF1\" />" +
"</Module>";
XElement module = XElement.Parse(xml);
Boolean simulation = module.Descendants("Parameter").Where(x => (x.Attribute("ID").Value == "Simulation") && (x.Attribute("Value").Value == "1")).Any();
}
}
}
答案 3 :(得分:0)
如果您已将XML加载到XDocument
中,则可以使用LINQ执行此操作:
var simulationExists = xDocument.Descendants("Parameter").Any(x => (string)x.Attribute("ID") == "Simulation");