Best way to delete and insert data into xml file

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

Sudip_inn

Guest
i have two xml file which identical but data could be different. so i need to find data from file1.xml by condition and remove that element if data found and find same data from file2.xml if found then i need to copy that element into file1.xml file.

i have many tables in my xml files.

see one records which give you idea.

<Broker ID="4E" Ticker_Id="ADBE" BrokerCategory="Contributing" Client="">
<Broker_Id>0</Broker_Id>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>BlueMatrix</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Model_Introduction</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Cover</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Drivers</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings>4Q 2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>IS</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings>4Q 2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>BS</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings>4Q 2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>CF</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings>4Q 2019</ReviewedEarnings>
<Pre-Post>Pre</Pre-Post>
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>FX</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Dates</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Variance</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Consensus</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Preview</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Valuation</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>CC Subscriptions &amp; ASPs</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Valuation_2</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>4Q16</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Summit2016 Preview</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>CS Seats</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>CC_New_Drivers</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>New Drivers</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>DCF</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Preview_OLD</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Estimates</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Drivers - Historical </TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Price Chart</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>CheetSheet</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Digital Marketing</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>PT Raise</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
<ALLTabsUnderBroker>
<Broker>4E</Broker>
<TAB>Charts</TAB>
<Commnet1 />
<Commnet2 />
<Broker_Id>0</Broker_Id>
<ReviewedEarnings />
</ALLTabsUnderBroker>
</Broker>

so i need to find in file1.xml file those broker element whose ID attribute has value say 4E like ID="4E" if found then i need to remove this records from file1.xml file and find the same records from another file2.xml file whose ID="4E" if found then i need to copy that broker element or that record from that file and add in file1.xml file.

does this approach work for me. here is the link What is the fastest way to combine two xml files into one

var xml1 = XDocument.Load("file1.xml");
var xml2 = XDocument.Load("file2.xml");

//Combine and remove duplicates
var combinedUnique = xml1.Descendants("AllNodes")
.Union(xml2.Descendants("AllNodes"));

//Combine and keep duplicates
var combinedWithDups = xml1.Descendants("AllNodes")
.Concat(xml2.Descendants("AllNodes"));


please guide me with example that how to achieve this with minimum line of code. thanks

Continue reading...
 
Back
Top