C#XML搜索和替换

时间:2016-12-12 22:08:31

标签: c# xml

我正在尝试搜索特定的XAttribute和XElement。

 <IcMDetails ServerGroup="ABC">
    <IncidentId>984513515</IncidentId>
    <Title>today is a great day</Title>
    <Description>this is a test</Description>
    <State>Active</State>
    <Severity>2</Severity>
    <DateCreated>2016-12-12</DateCreated>
  </IcMDetails>
 <IcMDetails ServerGroup="XYZ">
    <IncidentId>6845613</IncidentId>
    <Title>today is a great day</Title>
    <Description>this is a test</Description>
    <State>Active</State>
    <Severity>2</Severity>
    <DateCreated>2016-12-08</DateCreated>
  </IcMDetails>
 <IcMDetails ServerGroup="ABC">
    <IncidentId>12345345</IncidentId>
    <Title>today is a great day</Title>
    <Description>this is a test</Description>
    <State>Resolved</State>
    <Severity>2</Severity>
    <DateCreated>2016-12-08</DateCreated>
  </IcMDetails>

其中:    ServerGroup = ABC    State = Active

找到具体的儿童记录后,我想更新状态。我想我可以做类似的事情:

 public static void Update()
   {
     string filePath = "XML.xml"
     XDocument doc = XDocument.Load(filePath);

     var items = from i in doc.Descendants("IcMDetails")
        select i;

      foreach(var item in items)
        {
          item.Element("State").Value = "NewValue";
        }
   }

我使用以下内容来阅读XML文件。但是,我无法读取XAttribute值。

    public static void Read()
    {
        XmlTextReader reader = new XmlTextReader("TFS_Tickets.xml");

        while (reader.Read())
        {
            switch (reader.NodeType)
            {
                case XmlNodeType.Element: // The node is an element.
                    Console.Write("<" + reader.Name);
                    Console.WriteLine(">");
                    break;
                case XmlNodeType.Attribute:
                    Console.WriteLine(reader.Value);
                    break;                           
                case XmlNodeType.Text: //Display the text in each element.
                    Console.WriteLine(reader.Value);
                    break;
                case XmlNodeType.EndElement: //Display the end of the element.
                    Console.Write("</" + reader.Name);
                    Console.WriteLine(">");
                    break;
            }
        }

2 个答案:

答案 0 :(得分:1)

使用xml linq:

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("IcMDetails").Where(x => (string)x.Element("State") == "Active").ToList();

        }
    }
}

答案 1 :(得分:0)

这很简单,您可以使用LINQ的Enumerable.Where按条件过滤元素,然后更新它们。

var details = doc.Descendants("IcMDetails")
    .Where(x => (string) x.Attribute("ServerGroup") == "ABC" &&
                (string) x.Element("State") == "Active");

foreach (var detail in details)
{
    detail.SetElementValue("State", "NewValue");
}

您也可以在query syntax中编写相同的查询:

var details =
    from item in doc.Descendants("IcMDetails")
    where (string) item.Attribute("ServerGroup") == "ABC" &&
          (string) item.Element("State") == "Active"
    select item;

有关正常工作的演示,请参阅this fiddle