2016-04-04 12 views
0

My XML dosyası yapısı aşağıdaki gibidir:belirli nitelik değerini XML belgesine döngüye alınması ve arama

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

Ben XmlDocument kullanarak "Değerini" kontrol etmek istiyorum. Ben wan

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

dinamik bölümler içindeki tüm düğümler/eleman arama ve "Değer" kontrol etmek:

Yani böyle bir döngü yazdı. Lütfen öneriniz.

cevap

0

XPath'e bak [1] [2]. Basit ifadede çeşitli ölçütler kullanarak bir XML belgesinin belirli bir bölümüne başvurmanızı sağlar. Örneğin, ValueBoards kök element olup, veya //Boards//@Value aksi varsayarak /Boards//@Value olarak XPath'daki ifade edilebilir Boards elemanı içinde herhangi bir yerde nitelik bulabilirsiniz.

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

0

vedry kolayca XML Linq ile yapılabilir

XmlDocument kullanarak, sen SelectNodes() yöntemi ile XPath yürütebilirsiniz

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