none
How to add new element in xml file RRS feed

  • Question

  • i have so many elements like below one xml. in my xml files there are so many tables called

    TickerBrokerDateFormatMaps, TickerBrokerDateFormatMap 

    I need to add one more element called TickerBrokerDateFormatMap at the end of last element of TickerBrokerDateFormatMap 

    so how could i do it ?

      <TickerBrokerDateFormatMap BrokerTab_Id="0" Broker_Id="0" Ticker_Id="AMZN">
        <StandardDate>1Q 2010A</StandardDate>
        <ColumnCoordinate>X</ColumnCoordinate>
        <TickerBrokerDateFormatMaps_Id>0</TickerBrokerDateFormatMaps_Id>
        <BrokerDate StandardDate="1Q 2010A" Broker_Id="0" BrokerTab_Id="0">
          <year>1Q10</year>
          <Quater>1Q10</Quater>
        </BrokerDate>
      </TickerBrokerDateFormatMap>


    Now i want to add one more element like <TickerBrokerDateFormatMap> </TickerBrokerDateFormatMap> in xml file.

    how could i do it when loading file by XDocument  like XDocument xmlDoc = XDocument.Load(path);

    please share some example code. thanks


    I did it this way to add one more TickerBrokerDateFormatMap element at the end of or last TickerBrokerDateFormatMap element but no data saved in the xml file....where i made the mistake ?

                XDocument XDoc = XDocument.Load(xmlFilePath);
                var items = (from item in XDoc.Descendants("TickerBrokerDateFormatMap")
                             select item).ToList();

                XElement objNewTickerBrokerDateFormatMap = new XElement("TickerBrokerDateFormatMap",
                    new XAttribute("BrokerTab_Id", 1),
                    new XAttribute("Broker_Id", 2),
                    new XAttribute("Ticker_Id", "AMZN"),

                   new XElement("ColumnCoordinate", "DA"),
                       new XElement("BrokerDate",
                        new XElement("Year", "1Q 2021"),
                        new XElement("Quarter", "1Q 2021"),
                        new XElement("Tridip", "Test" + i)
                        )
                  );

                //XDoc.Element("TickerBroker").Element("TickerBrokerDateFormatMaps").Add(objNewTickerBrokerDateFormatMap);
                //items.Add(objNewTickerBrokerDateFormatMap);
                //XDoc.Add(items);
                XDoc.Root.Add(objNewTickerBrokerDateFormatMap);
                XDoc.Save(xmlFilePath);




    • Edited by Sudip_inn Monday, November 18, 2019 7:36 PM
    Monday, November 18, 2019 3:19 PM

Answers

  • Hi Sudip_inn,

    Thank you for posting here.

    For your question, you want to add a new element in xml file.

    Your code is basically right, I just made some minor changes.

     String path = @"d:\test\a.xml";
     XDocument XDoc = XDocument.Load(path);
    
     XElement objNewTickerBrokerDateFormatMap = new XElement("TickerBrokerDateFormatMap",
          new XAttribute("BrokerTab_Id", 1),
          new XAttribute("Broker_Id", 2),
          new XAttribute("Ticker_Id", "AMZN"),
    
          new XElement("ColumnCoordinate", "DA"),
          new XElement("BrokerDate",
          new XElement("Year", "1Q 2021"),
          new XElement("Quarter", "1Q 2021"),
          new XElement("Tridip", "Test" + 1)
          )
     );
     //Start at the root node and continue to the parent of the tickerbrokerdateformatmap.
     XDoc.Element("configuration").Element("TickerBroker").Add(objNewTickerBrokerDateFormatMap);
     XDoc.Save(path);

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Tuesday, November 19, 2019 3:52 PM
    Tuesday, November 19, 2019 6:22 AM

All replies

  • Hi Sudip_inn,

    Thank you for posting here.

    For your question, you want to add a new element in xml file.

    Your code is basically right, I just made some minor changes.

     String path = @"d:\test\a.xml";
     XDocument XDoc = XDocument.Load(path);
    
     XElement objNewTickerBrokerDateFormatMap = new XElement("TickerBrokerDateFormatMap",
          new XAttribute("BrokerTab_Id", 1),
          new XAttribute("Broker_Id", 2),
          new XAttribute("Ticker_Id", "AMZN"),
    
          new XElement("ColumnCoordinate", "DA"),
          new XElement("BrokerDate",
          new XElement("Year", "1Q 2021"),
          new XElement("Quarter", "1Q 2021"),
          new XElement("Tridip", "Test" + 1)
          )
     );
     //Start at the root node and continue to the parent of the tickerbrokerdateformatmap.
     XDoc.Element("configuration").Element("TickerBroker").Add(objNewTickerBrokerDateFormatMap);
     XDoc.Save(path);

    Result:

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Tuesday, November 19, 2019 3:52 PM
    Tuesday, November 19, 2019 6:22 AM
  • XDoc.Element("configuration").

    what is configuration in code ?

    Tuesday, November 19, 2019 3:52 PM
  • Hi Sudip_inn,

    Thanks for your feedback.

    Look at the picture, configuration is just the name of the root node in my xml file, it may be different from yours.

    Best Regards, 

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 20, 2019 1:12 AM