I'm not sure that I fully understand your code. I think that you should make more use of .Elements() rather than .Descendents(). The problem with .Descendents is that there are elements at different levels of your structure called "Broker".
There is TickerBrokerDateMap/Broker and also TickerBrokerMap/Broker/AllTabsUnderBroker/Broker which are treated as the same thing by Descendents
To find zipbrokerrow the element for a particular broker I have changed your line to
Participant
1660 Points
952 Posts
Re: How to add xml element at specific position in multiple records of xml file
Dec 15, 2019 10:11 PM|PaulTheSmith|LINK
I'm not sure that I fully understand your code. I think that you should make more use of .Elements() rather than .Descendents(). The problem with .Descendents is that there are elements at different levels of your structure called "Broker".
There is TickerBrokerDateMap/Broker and also TickerBrokerMap/Broker/AllTabsUnderBroker/Broker which are treated as the same thing by Descendents
To find zipbrokerrow the element for a particular broker I have changed your line to
zipbrokerrow = xmlDocZip.XPathSelectElements("TickerBrokerDateMap/Broker").FirstOrDefault( ...etc...);
At the end of the code when you are appending the newly created element the code needs to be something like
xmlDocZip.XPathSelectElements("TickerBrokerDateMap/Broker").Last().AddAfterSelf(brokerelement);
(BTW - The code that you have shown seems to create a duplicate element but I assume that is just because it is an example and not the real code)
(BTW 2 - Why have the word 'zip' in variable names when there is no zipping/compression going on? - fooled me!)