locked
How to generate xml file using XDocument in loop RRS feed

  • Question

  • here is my code which is not throwing error but output xml structure is not right

    here is code

    XDocument xDoc = new XDocument(new XElement("TickerBrokerStandardDateLineitem",
                        /*new XDeclaration("1.0", "utf-8", "yes"),*/
                        new XElement("Ticker"),
                        new XElement("TickerID"),
                        new XElement("TickerBrokerStandardDateLineitemValues")));
    
                        for (int x = 0; x <= TenQKbogeyData.Rows.Count - 1;x++ )
                        {
                            xDoc.Root.Add(new XElement("TickerBrokerStandardDateLineitemValue",
                                new XElement("TabName", TenQKbogeyData.Rows[x]["TabName"]),
                                new XElement("StandardDate", TenQKbogeyData.Rows[x]["StandardDate"]),
                                new XElement("XFundCode", TenQKbogeyData.Rows[x]["XFundCode"]),
                                new XElement("BRTab", TenQKbogeyData.Rows[x]["BRTab"]),
                                new XElement("BRLineItem", TenQKbogeyData.Rows[x]["BRLineItem"]),
                                new XElement("StandardLineItem", TenQKbogeyData.Rows[x]["StandardLineItem"]),
                                new XElement("ActualProvidedByCompany", TenQKbogeyData.Rows[x]["ActualProvidedByCompany"])));
                        }
    
                        xDoc.Save(strpath);

    TenQKbogeyData is data table and i am iterate in loop for data table and adding data

    all TickerBrokerStandardDateLineitemValue will be inside of TickerBrokerStandardDateLineitemValues root node

    but in my case TickerBrokerStandardDateLineitemValues is empty at top which is wrong. so guide me how to write the code.

    thanks

    Wednesday, April 15, 2020 2:00 PM

Answers

  • i sort the issue this way

    XDocument xDoc = new XDocument(new XElement("TickerBrokerStandardDateLineitem",
                        new XElement("Ticker"),
                        new XElement("TickerID")
                        ));
    
                        xDoc.Root.Add(new XElement("TickerBrokerStandardDateLineitemValues"));
    
                        for (int x = 0; x <= TenQKbogeyData.Rows.Count - 1;x++ )
                        {
                            xDoc.Root.Element("TickerBrokerStandardDateLineitemValues").Add(new XElement("TickerBrokerStandardDateLineitemValue",
                                new XElement("TabName", TenQKbogeyData.Rows[x]["TabName"]),
                                new XElement("StandardDate", TenQKbogeyData.Rows[x]["StandardDate"]),
                                new XElement("XFundCode", TenQKbogeyData.Rows[x]["XFundCode"]),
                                new XElement("BRTab", TenQKbogeyData.Rows[x]["BRTab"]),
                                new XElement("BRLineItem", TenQKbogeyData.Rows[x]["BRLineItem"]),
                                new XElement("StandardLineItem", TenQKbogeyData.Rows[x]["StandardLineItem"]),
                                new XElement("ActualProvidedByCompany", TenQKbogeyData.Rows[x]["ActualProvidedByCompany"])));
                        }
    
                        xDoc.Save(strpath);
                        xDoc = null;

    • Marked as answer by Sudip_inn Wednesday, April 15, 2020 4:38 PM
    Wednesday, April 15, 2020 2:24 PM