【问题标题】:C# XML Search and ReplaceC# XML 搜索和替换
【发布时间】:2016-12-12 22:08:30
【问题描述】:

我正在尝试搜索特定的 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>

在哪里: 服务器组 = ABC 状态 = 活跃

找到特定的子记录后,我想更新状态。我想我可以这样做:

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

【问题讨论】:

    标签: c# xml


    【解决方案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();
    
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      这相当简单,您可以使用 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 以获得工作演示。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-06-12
        • 2017-02-03
        • 2012-07-08
        • 2015-08-28
        • 2020-08-24
        • 1970-01-01
        • 2020-07-20
        相关资源
        最近更新 更多