C#XML节点搜索属性和返回节点值

时间:2016-12-19 17:40:00

标签: c# xml

我有以下xml。我正在尝试搜索特定节点(即IcMDetails Cluster =“”)并返回节点级别详细信息。

我所拥有的代码将找到一个特定的集群(即IcMDetails Cluster =“ClusterA”)。但是,我无法弄清楚如何获取节点级详细信息(即IncidientId,Title,AssignedTo等)并将这些详细信息分配给我可以在另一个函数中使用的变量。

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

1 个答案:

答案 0 :(得分:0)

使用Linq

btw ...注意样本xml中的拼写错误 <IncidnetId>0000002</IncidnetId>事件拼写错误

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


    }
}