2016-04-04 31 views
0

Meine XML-Dateistruktur wie folgt aussieht:Looping durch XML-Dokument und suchen ein bestimmtes Attribut Wert

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

Ich möchte den „Wert“ überprüfen XmlDocument verwenden.

Also schrieb ich eine Schleife wie folgt aus:

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

I wan dynamisch innerhalb Boards alle Knoten/Element suchen und die "Value" überprüfen. Bitte vorschlagen.

Antwort

0

Schauen Sie in XPath [1] [2]. Es ermöglicht Ihnen, einen bestimmten Teil eines XML-Dokuments unter Verwendung verschiedener Kriterien in einem einfachen Ausdruck zu referenzieren. Zum Beispiel finden Value überall innerhalb Boards Element Attribut kann in XPath als /Boards//@Value ausgedrückt werden unter der Annahme, dass Boards das Wurzelelement ist, oder //Boards//@Value anders.

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

Wenn XmlDocument verwenden, können Sie XPath über SelectNodes() Methode ausführen http://www.w3schools.com/xsl/xpath_syntax.asp

0

mit XML Linq kann getan werden vedry leicht

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