循环遍历XML文档并搜索特定的属性值

时间:2016-04-04 12:02:32

标签: c# xml

我的XML文件结构如下所示:

<Boards CountBoards="3" name="NAME" info="INFO" otherInfo="OTHERINFO">
  <Board0 Name="Alm_HP_RE" BoardWidth="1800" BoardHeigth="1800" Image="" ImageLayout="None" fileName="FILENAME" path="PATH">
    <Controls CountControls="2">
      <IOControl Type="DigitalInput" Name=".._M1AED10_GS110_XG001" GroupName="" ControlBackColor="LimeGreen" ControlTextColor="White" ControlVisible="True" ControlWidth="207" ControlHeigth="26" IOMapName="IF3.ST1.IF1.ST3.DigitalInput01" IsHidden="False" DisplayElementVisible="True" GraphVisible="False" NameLabelVisible="True" LableContent="PV_Name" DiagramBackColor="Black" DiagramLineColor="Red" LineWidth="2" Timescale="225" XGrid="9" YGrid="4" Value="9" MaxYValue="2" MinYValue="-2" FeedBackSwitch="" FeedBackValue="" FeedBackColor="White" Signal="">
        <Position X="259" Y="281" />
      </IOControl>
      <IOControl Type="DigitalInput" Name="IF3.ST1.IF1.ST3.DigitalInput08" GroupName="" ControlBackColor="LimeGreen" ControlTextColor="White" ControlVisible="True" ControlWidth="217" ControlHeigth="26" IOMapName="IF3.ST1.IF1.ST3.DigitalInput08" IsHidden="False" DisplayElementVisible="True" GraphVisible="False" NameLabelVisible="True" LableContent="PV_Name" DiagramBackColor="Black" DiagramLineColor="Red" LineWidth="2" Timescale="225" XGrid="9" YGrid="4" Value="9" MaxYValue="2" MinYValue="-2" FeedBackSwitch="" FeedBackValue="" FeedBackColor="White" Signal="">
        <Position X="313" Y="199" />
      </IOControl>
    </Controls>
  </Board0>
  <Board1 Name="AO_Exchange" BoardWidth="1800" BoardHeigth="1800" Image="" ImageLayout="None" fileName="FILENAME" path="PATH">
    <Controls CountControls="2">
      <IOControl Type="DigitalInput" Name=".._M0CBC01_F151_XG001" GroupName="" ControlBackColor="LimeGreen" ControlTextColor="White" ControlVisible="True" ControlWidth="199" ControlHeigth="26" IOMapName="IF3.ST1.IF1.ST5.DigitalInput05" IsHidden="False" DisplayElementVisible="True" GraphVisible="False" NameLabelVisible="True" LableContent="PV_Name" DiagramBackColor="Black" DiagramLineColor="Red" LineWidth="2" Timescale="225" XGrid="9" YGrid="4" Value="0" MaxYValue="2" MinYValue="-2" FeedBackSwitch="" FeedBackValue="" FeedBackColor="White" Signal="">
        <Position X="130" Y="260" />
      </IOControl>
      <IOControl Type="DigitalInput" Name=".._M0CBC01_F5152_XG001" GroupName="" ControlBackColor="LimeGreen" ControlTextColor="White" ControlVisible="True" ControlWidth="205" ControlHeigth="26" IOMapName="IF3.ST1.IF1.ST5.DigitalInput12" IsHidden="False" DisplayElementVisible="True" GraphVisible="False" NameLabelVisible="True" LableContent="PV_Name" DiagramBackColor="Black" DiagramLineColor="Red" LineWidth="2" Timescale="225" XGrid="9" YGrid="4" Value="0" MaxYValue="2" MinYValue="-2" FeedBackSwitch="" FeedBackValue="" FeedBackColor="White" Signal="">
        <Position X="201" Y="463" />
      </IOControl>
    </Controls>
  </Board1>
  <Board2 Name="DO_Exchange" BoardWidth="1800" BoardHeigth="1800" Image="" ImageLayout="None" fileName="FILENAME" path="PATH">
    <Controls CountControls="2">
      <IOControl Type="DigitalInput" Name=".._M0CBC01_F10_XG001" GroupName="" ControlBackColor="LimeGreen" ControlTextColor="White" ControlVisible="True" ControlWidth="192" ControlHeigth="26" IOMapName="IF3.ST1.IF1.ST5.DigitalInput07" IsHidden="False" DisplayElementVisible="True" GraphVisible="False" NameLabelVisible="True" LableContent="PV_Name" DiagramBackColor="Black" DiagramLineColor="Red" LineWidth="2" Timescale="225" XGrid="9" YGrid="4" Value="0" MaxYValue="2" MinYValue="-2" FeedBackSwitch="" FeedBackValue="" FeedBackColor="White" Signal="">
        <Position X="251" Y="194" />
      </IOControl>
      <IOControl Type="DigitalInput" Name=".._M0CBC01_F152_XG001" GroupName="" ControlBackColor="LimeGreen" ControlTextColor="White" ControlVisible="True" ControlWidth="199" ControlHeigth="26" IOMapName="IF3.ST1.IF1.ST5.DigitalInput06" IsHidden="False" DisplayElementVisible="True" GraphVisible="False" NameLabelVisible="True" LableContent="PV_Name" DiagramBackColor="Black" DiagramLineColor="Red" LineWidth="2" Timescale="225" XGrid="9" YGrid="4" Value="0" MaxYValue="2" MinYValue="-2" FeedBackSwitch="" FeedBackValue="" FeedBackColor="White" Signal="">
        <Position X="186" Y="113" />
      </IOControl>
    </Controls>
  </Board2>
</Boards>

我想检查&#34;价值&#34;使用XmlDocument。

所以我写了一个这样的循环:

 XmlDocument iosDoc = new XmlDocument();
 iosDoc.Load(ios[0].FullName);
 XmlNodeList boardList = iosDoc.GetElementsByTagName("Boards");
 foreach (XmlNode node in boardList)
 {
  foreach (XmlNode xc in node.ChildNodes)
  { // wahat need to check here, I am not getting.
  }
 }

我想动态搜索Boards中的所有节点/元素并检查&#34; Value&#34;。 请建议。

2 个答案:

答案 0 :(得分:0)

了解XPath [1] [2] 。它使您能够使用简单表达式中的各种条件引用XML文档的特定部分。例如,Value元素中的任何位置查找Boards属性可以在XPath中表示为 /Boards//@Value ,假设Boards是根元素,否则为 //Boards//@Value

使用XmlDocument时,您可以通过SelectNodes()方法执行XPath:

XmlDocument iosDoc = new XmlDocument();
iosDoc.Load(ios[0].FullName);
var result = doc.SelectNodes("//Boards//@Value");
foreach (XmlAttribute item in result)
{
    Console.WriteLine(item.Value);
}

[1]:https://www.w3.org/TR/xpath/
[2]:http://www.w3schools.com/xsl/xpath_syntax.asp

答案 1 :(得分:0)

Can be done with XML Linq vedry easily

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            var results = doc.Descendants("Boards").Elements().Select(x => new
            {
                name = x.Name.LocalName,
                board = x,
                ioControls = x.Descendants("IOControl").Select(y => new {
                    control = y,
                    value = y.Attribute("Value")
                }).ToList()
            }).ToList();
        }
    }
}