none
How to update and add element in xml file using XDocument RRS feed

  • Question

  • This way i update data in xml file

    XDocument xmlDoc = XDocument.Load(strPath);
    
    var items = (from item in xmlDoc.Descendants("clsCommentPopup")
    	     where item.Element("Section").Value.Trim().ToUpper() == section.Trim().ToUpper()
    	     && item.Element("LineItem").Value.Trim().ToUpper() == li.Trim().ToUpper()
    	     select item).ToList();
    
    bool IsFieldUpdated = false;
    
    foreach (var item in items)
    {
        if (ignorecelldata != "")
        {
    	if (_IsIgnored)
    	    item.Element("IgnoreData").SetValue(ignorecelldata);
    	else
    	    item.Element("IgnoreData").SetValue(string.Empty);
    
    	IsFieldUpdated = true;
        }
    }
    
    if (IsFieldUpdated)
        xmlDoc.Save(strCommentPath);

    now i have to also search for ID element in each row by section and lineitem. if match found then i have to search ID element. if not found then i need to insert ID element in matched row where i will set a value for ID.

    how to achieve it with XDocument. if possible discuss with a example code. thanks

    Saturday, September 14, 2019 11:24 AM

Answers

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

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

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