none
How to add and update element in xml file based match found after search RRS feed

  • Question

  • suppose my xml look like 

    <?xml version="1.0" encoding="utf-8"?>
    <TickerBrokerStandardDateLineitem>
      <Ticker />
      <TickerID />
      <TickerBrokerStandardDateLineitemValues>
        <TickerBrokerStandardDateLineitemValue>
          <TabName>Consensus Model</TabName>
          <StandardDate>2010 FY</StandardDate>
          <XFundCode>TRIN</XFundCode>
          <BRTab></BRTab>
          <BRLineItem></BRLineItem>
          <StandardLineItem>Total Sales</StandardLineItem>
          <StandardValue></StandardValue>
          <ActualProvidedByCompany>NO</ActualProvidedByCompany>
        </TickerBrokerStandardDateLineitemValue>
        <TickerBrokerStandardDateLineitemValue>
          <TabName>Consensus Model</TabName>
          <StandardDate>2011 FY</StandardDate>
          <XFundCode>TRIN</XFundCode>
          <BRTab></BRTab>
          <BRLineItem></BRLineItem>
          <StandardLineItem>Total Sales</StandardLineItem>
          <StandardValue></StandardValue>
          <ActualProvidedByCompany>NO</ActualProvidedByCompany>
        </TickerBrokerStandardDateLineitemValue>
      </TickerBrokerStandardDateLineitemValues>
    </TickerBrokerStandardDateLineitem>

    now i have to update XFundCode in all records of xml file if my supplied TabName & StandardLineItem match those records

    also i have to add one element called ID=55 to each records if my supplied TabName & StandardLineItem match those records

    so i have two task that i have to add one element called ID = 55 to each records if my TabName & StandardLineItem found in those records of xml file & i have to update also XFundCode element if my TabName & StandardLineItem found in those records of xml file.

    Please share sample code to achieve this task. thanks



    • Edited by Sudip_inn Saturday, September 14, 2019 6:05 AM
    Saturday, September 14, 2019 6:04 AM

Answers

  • This code works

    XDocument xmlDoc = XDocument.Load(@"D:\test.xml");
    
                var items = (from item in xmlDoc.Descendants("TickerBrokerStandardDateLineitemValue")
                             where item.Element("TabName").Value.Trim() == "Consensus Model"
                             && item.Element("StandardLineItem").Value.Trim() == "Total Sales"
                             select item).ToList();
    
                bool IsFieldUpdated = false;
    
                foreach (var item in items)
                {
                    if (item.Element("ID") == null)
                    {
                        item.Add(new XElement("ID", 55));
                    }
    
                    if (item.Element("XFundCode") != null)
                    {
                        item.Element("XFundCode").SetValue("Test");
                    }
                }
                xmlDoc.Save(@"D:\test.xml");
    looking for more better way out. thanks

    • Marked as answer by Sudip_inn Sunday, September 15, 2019 7:41 PM
    Saturday, September 14, 2019 12:00 PM