繁体   English   中英

在XML中,如何使用C#检查子节点是否包含特定值

[英]In XML, how to check if child nodes contain specific values with C#

  <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>

现在,我想搜索其中是否存在“模拟”,我该怎么做。 请提出建议。

尝试这个..

var doc_xml = new XmlDocument();
//load your xmlfile
doc_xml.Load(@"..\Yourxmlfile.xml");

foreach (XmlNode node in doc_xml.DocumentElement.ChildNodes)
{
//your code
}

使用XPath是一种方法:

var xDoc = XDocument.Load("test.xml");
var element = xDoc.XPathSelectElement("Module/Parameter[@ID='Simulation']");
if (element != null)
{
    // Simulation is present
}

使用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();
        }
    }
}
​

如果您已将XML加载到XDocument ,则可以使用LINQ这样做:

var simulationExists = xDocument.Descendants("Parameter").Any(x => (string)x.Attribute("ID") == "Simulation");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM