2016-12-19 2 views
0

다음 XML이 있습니다. 특정 노드 (예 : IcMDetails Cluster = "")를 검색하고 노드 수준 세부 정보를 반환하려고합니다.C# XML 노드 검색 속성 및 반환 노드 값

코드에서 특정 클러스터를 찾을 수 있습니다 (예 : IcMDetails Cluster = "ClusterA"). 그러나 노드 레벨 세부 정보 (예 : IncidientId, Title, AssignedTo 등)를 가져 오는 방법을 파악할 수 없어 다른 기능에서 사용할 수있는 변수에 세부 정보를 할당 할 수 없습니다. BTW ... 샘플 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