Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在XML中,如何使用C检查子节点是否包含特定值#_C#_.net_Xml - Fatal编程技术网

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

C# 在XML中,如何使用C检查子节点是否包含特定值#,c#,.net,xml,C#,.net,Xml,现在我想搜索,如果“模拟”存在于其中,我如何才能做到这一点。 请建议。试试这个 <Module Name="CPULIST01" Type="CPULIST" Version="1.1.0.0" Description="UNIT1.2"> <Connector Name="IF2" NodeNumber="4"> <Parameter ID="ActivateDevice" Value="1" /> <Paramet

现在我想搜索,如果“模拟”存在于其中,我如何才能做到这一点。 请建议。

试试这个

  <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>
试试这个

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

使用XPath是一种方法:

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 doc_xml = new XmlDocument();
//load your xmlfile
doc_xml.Load(@"..\Yourxmlfile.xml");

foreach (XmlNode node in doc_xml.DocumentElement.ChildNodes)
{
//your code
}
使用XML-Linq

var xDoc = XDocument.Load("test.xml");
var element = xDoc.XPathSelectElement("Module/Parameter[@ID='Simulation']");
if (element != null)
{
    // Simulation is present
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xml=
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
XElement模块=XElement.Parse(xml);
布尔模拟=module.subjects(“参数”)。其中(x=>(x.Attribute(“ID”).Value==“模拟”)&(x.Attribute(“Value”)。Value==“1”)。任意();
}
}
}
​
使用XML Linq

var xDoc = XDocument.Load("test.xml");
var element = xDoc.XPathSelectElement("Module/Parameter[@ID='Simulation']");
if (element != null)
{
    // Simulation is present
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xml=
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"" +
"";
XElement模块=XElement.Parse(xml);
布尔模拟=module.subjects(“参数”)。其中(x=>(x.Attribute(“ID”).Value==“模拟”)&(x.Attribute(“Value”)。Value==“1”)。任意();
}
}
}
​

如果您已将XML加载到
XDocument
中,则可以使用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执行以下操作:

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

1.这实际上并没有解决OP的问题——它只是展示了如何通过document元素的子节点循环TTR;2.它只遍历document元素的直接子元素-如果ID属性值为
Simulation
的Parameter元素是
Connector
元素的子元素,则它不会是循环中的
节点
值之一。1。这实际上并没有解决OP的问题——它只是展示了如何通过document元素的子节点循环TTR;2.它只遍历document元素的直接子元素-如果ID属性值为
Simulation
的Parameter元素是
Connector
元素的子元素,则它不会是循环中的
节点
值之一。ID为
Simulation
的元素是否可以是的子元素一个
连接器
元素,还是它总是将成为
模块
元素的子元素?ID为
模拟
的元素可以是
连接器
元素的子元素,还是它总是将成为
模块
元素的子元素?欢迎,请随意投票+接受答案。欢迎,请随意投票并接受答案。