How to add xml element at specific position in multiple records of xml file

  • Thread starter Thread starter Sudip_inn
  • Start date Start date
S

Sudip_inn

Guest
i have large xml file which has multiple tables. i want to add one element at specific position. i tried but in my case element is getting added at last which is not desirable.

my tables are. TickerBroker , Broker, BrokerTab and TickerBrokerDateFormatMap

i have multiple Broker element and when i am adding a new Broker then it is getting added in xml file after TickerBrokerDateFormatMap element at the end. i want to add Broker element at the end of last Broker element in xml file.

if no Broker element exist in xml file then my added Broker element should be adding at the end of last TickerBroker element.

here i am giving my sample xml data which is bit big file.

<?xml version="1.0" standalone="yes"?>
<TickerBrokerDateMap>
<TickerBroker>
<Ticker_Id>ADBE</Ticker_Id>
<Ticker_Id_auto>ADBE</Ticker_Id_auto>
</TickerBroker>

<Broker ID="MV-P1" Ticker_Id="ADBE" BrokerCategory="Cascade" Client="">
<Broker_Id>25</Broker_Id>
<ALLTabsUnderBroker>
<Broker>MV-P1</Broker>
<TAB>QIS</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>25</Broker_Id>
<ReviewedEarnings>3Q 2019</ReviewedEarnings>
<Pre-Post>Post</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>MV-P1</Broker>
<TAB>BS</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>25</Broker_Id>
<ReviewedEarnings>3Q 2019</ReviewedEarnings>
<Pre-Post>Post</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>MV-P1</Broker>
<TAB>METRICS</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>25</Broker_Id>
<ReviewedEarnings>3Q 2019</ReviewedEarnings>
<Pre-Post>Post</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>MV-P1</Broker>
<TAB>ESTIMATE</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>25</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>MV-P1</Broker>
<TAB>EPS</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>25</Broker_Id>
<ReviewedEarnings>3Q 2019</ReviewedEarnings>
<Pre-Post>Post</Pre-Post>
</ALLTabsUnderBroker>
</Broker>
<Broker ID="UN" Ticker_Id="ADBE" BrokerCategory="Contributing" Client="">
<Broker_Id>27</Broker_Id>
<ALLTabsUnderBroker>
<Broker>UN</Broker>
<TAB>ModelCover</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>27</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>UN</Broker>
<TAB>Assumptions</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>27</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>UN</Broker>
<TAB>Revenue Build</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>27</Broker_Id>
<ReviewedEarnings>4Q2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>UN</Broker>
<TAB>Segment Breakout</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>27</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>UN</Broker>
<TAB>Geographic Breakout</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>27</Broker_Id>
<ReviewedEarnings>4Q2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>UN</Broker>
<TAB>Calcs</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>27</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>UN</Broker>
<TAB>Debt</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>27</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>UN</Broker>
<TAB>GAAP Income Statement</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>27</Broker_Id>
<ReviewedEarnings>4Q2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>UN</Broker>
<TAB>Non-GAAP Income Statement</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>27</Broker_Id>
<ReviewedEarnings>4Q2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>UN</Broker>
<TAB>Balance Sheet</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>27</Broker_Id>
<ReviewedEarnings>4Q2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>UN</Broker>
<TAB>Cash Flow</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>27</Broker_Id>
<ReviewedEarnings>4Q2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>UN</Broker>
<TAB>Disclosures</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>27</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
</Broker>
<Broker ID="BW" Ticker_Id="ADBE" BrokerCategory="NonContributing" Client="">
<Broker_Id>28</Broker_Id>
<ALLTabsUnderBroker>
<Broker>BW</Broker>
<TAB>Adobe Model_Stifel</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>28</Broker_Id>
<ReviewedEarnings>4Q 2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>BW</Broker>
<TAB>Disclaimer (Read first)</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>28</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>BW</Broker>
<TAB>Sources</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>28</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
</Broker>
<Broker ID="PJ-C1" Ticker_Id="ADBE" BrokerCategory="Contributing" Client="">
<Broker_Id>29</Broker_Id>
<ALLTabsUnderBroker>
<Broker>PJ-C1</Broker>
<TAB>IS</TAB>
<Commnet1>Use Custom</Commnet1>
<Commnet2>Use Custom</Commnet2>
<Broker_Id>29</Broker_Id>
<ReviewedEarnings>4Q 2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>PJ-C1</Broker>
<TAB>BS</TAB>
<Commnet1>Use Custom</Commnet1>
<Commnet2>Use Custom</Commnet2>
<Broker_Id>29</Broker_Id>
<ReviewedEarnings>4Q 2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>PJ-C1</Broker>
<TAB>CF</TAB>
<Commnet1>Use Custom</Commnet1>
<Commnet2>Use Custom</Commnet2>
<Broker_Id>29</Broker_Id>
<ReviewedEarnings>4Q 2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>PJ-C1</Broker>
<TAB>Segment Breakout</TAB>
<Commnet1>Use Custom</Commnet1>
<Commnet2>Use Custom</Commnet2>
<Broker_Id>29</Broker_Id>
<ReviewedEarnings>4Q 2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
</Broker>
<BrokerTab name="IS" Broker_Id="0" Ticker_Id="ADBE" Revise_Date="11-19-2019">
<BrokerTab_Id>0</BrokerTab_Id>
<Tab_index>0</Tab_index>
<save_flag>true</save_flag>
<isUSed>true</isUSed>
</BrokerTab>
<BrokerTab name="Drivers" Broker_Id="0" Ticker_Id="ADBE" Revise_Date="11-19-2019">
<BrokerTab_Id>1</BrokerTab_Id>
<Tab_index>1</Tab_index>
<save_flag>true</save_flag>
<isUSed>true</isUSed>
</BrokerTab>
<BrokerTab name="BS" Broker_Id="0" Ticker_Id="ADBE" Revise_Date="11-19-2019">
<BrokerTab_Id>2</BrokerTab_Id>
<Tab_index>2</Tab_index>
<save_flag>true</save_flag>
<isUSed>true</isUSed>
</BrokerTab>
<BrokerTab name="CF" Broker_Id="0" Ticker_Id="ADBE" Revise_Date="11-19-2019">
<BrokerTab_Id>3</BrokerTab_Id>
<Tab_index>3</Tab_index>
<save_flag>true</save_flag>
<isUSed>true</isUSed>
</BrokerTab>
<TickerBrokerDateFormatMap BrokerTab_Id="154" Broker_Id="29" Ticker_Id="ADBE">
<StandardDate>2021 FYE</StandardDate>
<ColumnCoordinate>AQ</ColumnCoordinate>
<TickerBrokerDateFormatMaps_Id>168</TickerBrokerDateFormatMaps_Id>
<BrokerDate StandardDate="2021 FYE" Broker_Id="29" BrokerTab_Id="154">
<year>2021 FYE</year>
<Quater>2021 FYE</Quater>
</BrokerDate>
</TickerBrokerDateFormatMap>
</TickerBrokerDateMap>

here i am giving my c# code to add xml broker element which is adding at the end of file which is not desirable.

string srclocalfile = @"C:\FILE1.xml";
string strZipBrokerID = "10";
string strSourceBrokerID = "12";
string broker = "UN";

XDocument xmlDocZip = XDocument.Load(srclocalfile);

var zipbrokerrow = xmlDocZip.Descendants().Elements("Broker").FirstOrDefault(b => b.Attribute("ID").Value.ToString().Trim().Equals(broker));

if (zipbrokerrow != null)
{

XElement oSubElm = null;
XElement brokerelement = new XElement("Broker",
new XAttribute("ID", broker),
new XAttribute("Ticker_Id", strTicker),
new XAttribute("BrokerCategory", zipbrokerrow.Attribute("BrokerCategory").Value),
new XAttribute("Client", zipbrokerrow.Attribute("Client").Value),
new XElement("Broker_Id", strSourceBrokerID));

foreach (XElement e in zipbrokerrow.Descendants("ALLTabsUnderBroker"))
{
string _broker = e.Element("Broker").Value;
string _TAB = e.Element("TAB").Value;
string _Commnet1 = e.Element("Commnet1").Value;
string _Commnet2 = e.Element("Commnet2").Value;
string _Broker_Id = e.Element("Broker_Id").Value;
string _ReviewedEarnings = e.Element("ReviewedEarnings").Value;

oSubElm = new XElement("ALLTabsUnderBroker",
new XElement("Broker", _broker),
new XElement("TAB", _TAB),
new XElement("Commnet1", _Commnet1),
new XElement("Commnet2", _Commnet2),
new XElement("Broker_Id", _Broker_Id),
new XElement("ReviewedEarnings", _ReviewedEarnings));

brokerelement.Add(oSubElm);

}

xmlDocZip.Root.Descendants("Broker").FirstOrDefault().Parent.Add(brokerelement);
xmlDocZip.Save(srclocalfile);
}

see i am using XDocument class. so please post your sample code using XDocument. thanks

Continue reading...
 
Back
Top