2016-12-19 4 views
0

Ich habe die folgende XML. Ich versuche, nach einem bestimmten Knoten (d. H. IcMDetails Cluster = "") zu suchen und die Details auf Knotenebene zurückzugeben.C# XML-Node-Suchattribut und Return-Node-Werte

Der Code, den ich habe, wird einen bestimmten Cluster (d. H. IcMDetails Cluster = "ClusterA") finden. Ich bin jedoch nicht in der Lage, herauszufinden, wie die Details auf Knotenniveau (d. H. IncidientId, Title, AssignedTo usw.) erhalten werden, und weise diese Details Variablen zu, die ich in einer anderen Funktion verwenden kann.

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

-Code

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

Antwort

0

Mit Linq

btw ... watch out für die Tippfehler in Ihrer Probe xml <IncidnetId>0000002</IncidnetId> Vorfall

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

Danke für die Hilfe blase_125 . – emie