2016-12-19 23 views
0

私は以下のXMLを持っています。特定のノード(IcMDetails Cluster = "")を検索し、ノードレベルの詳細を返そうとしています。C#XMLノードの検索属性と戻り値の値

コードで特定のクラスター(IcMDetails Cluster = "ClusterA"など)が見つかります。しかし、ノードレベルの詳細(IncidientId、Title、AssignedToなど)を取得し、別の関数で使用できる変数にこれらの詳細を割り当てる方法を見つけることができません。ところで...あなたのサンプルXML <IncidnetId>0000002</IncidnetId>事件のタイプミスに気をつけLINQの

使用

<CapacityIssues xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <IcMDetails Cluster="ClusterA"> 
     <IncidentId>0000001</IncidentId> 
     <Title>Capacity Issues AA2</Title> 
     <AssignedTo>DCIM</AssignedTo> 
     <Description>Cluster A OFR % is X with only 5 Empty nodes.</Description> 
     <State>Active</State> 
     <Severity>2</Severity> 
     <DateCreated>2016-09-10</DateCreated> 
    </IcMDetails> 
    <IcMDetails Cluster="ClusterB"> 
      <IncidnetId>0000002</IncidnetId> 
      <Title>Capacity Issues AA2</Title> 
      <AssignedTo>DCMx</AssignedTo> 
      <Description>This is a test</Description> 
      <State>Active</State> 
      <Severity>0</Severity> 
      <DateCreated>2016-10-10</DateCreated> 
    </IcMDetails> 
</CapacityIssues> 

コード

public static void Update() 
    { 
     XmlTextReader reader = new XmlTextReader(filePath); 

     while (reader.Read()) 
     { 
      try 
      { 
       if (reader.HasAttributes) 
       { 
        for (int i = 0; i < reader.AttributeCount; i++) 
        { 
         string s = reader[i]; 

         bool contains = s != null && s.Contains("ClusterA"); 

         if (contains) 
         { 
          Console.WriteLine(" {0} and {1}", s, true); 
         } 
        } 
        reader.MoveToElement(); 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine($"Cannot load XML, failures detected ruleset as {e.Message}"); 
      } 
     } 
    } 

答えて

0

は支援blase_125ため

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

namespace XMLNodeAttributeSearch_41228129 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      string filepath = @"M:\StackOverflowQuestionsAndAnswers\XMLNodeAttributeSearch_41228129\XMLNodeAttributeSearch_41228129\sample.xml"; 
      Update(filepath); 
     } 

     public static void Update(string incomingFilePath) 
     { 
      XDocument theDoc = XDocument.Load(incomingFilePath, LoadOptions.None); 

      List<XElement> ClusterElements = theDoc.Descendants("IcMDetails").ToList(); 
      foreach (XElement item in ClusterElements) 
      { 
       if (item.Attribute((XName)"Cluster").Value == "ClusterA") 
       { 
        string incidentid = item.Element((XName)"IncidentId").Value; 
        Console.WriteLine(incidentid); 
       } 
       else if (item.Attribute((XName)"Cluster").Value == "ClusterB") 
       { 
        string incidentid = item.Element((XName)"IncidentId").Value; 
        Console.WriteLine(incidentid); 
       } 
      } 
      Console.ReadLine(); 
     } 


    } 
} 
+0

感謝を綴りに誤りがあります。 – emie

関連する問題