none
Best way to delete and insert data into xml file RRS feed

  • Question

  • 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 https://stackoverflow.com/a/982698

    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



    • Edited by Sudip_inn Thursday, December 12, 2019 8:23 PM
    Thursday, December 12, 2019 7:54 PM

Answers

  • You have the basic idea, I think, but there are some details.  One thing to notice is the <Broker_Id> record in there; it's not a pure list of ALLTabsUnderBrokers tags, so the Union/Intersection thing isn't quite right.

    However, you can extract the ALLTabsUnderBrokers tags from the two lists and manipulate them.  For example:

        var xml1 = XElement.Load("file1.xml");
        IEnumerable<XElement> xml1recs = xml1.Elements("ALLTabsUnderBroker")
        var xml2 = XElement.Load("file2.xml");
        IEnumerable<XElement> xml2recs = xml2.Elements("ALLTabsUnderBroker");

    Now, xml1recs and xml2recs are enumerable lists of just those records.  So, at that point, you should be able to do

        var unique = xml1recs.Union(xml2recs);

    but I couldn't get that to work in a few minutes.  Even if I supply an IEqualityComparer, it doesn't get called.  But that's the right direction.

    When you reconstruct the document, you'd need to re-add the <Broer_Id> tag.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.


    • Edited by Tim Roberts Friday, December 13, 2019 1:04 AM
    • Marked as answer by Sudip_inn Friday, December 13, 2019 6:55 AM
    Friday, December 13, 2019 1:04 AM
  • Hi Sudip_inn,

    Thank you for posting here.

    I made some modifications based on the xml content you gave, and this is a code example.

            tatic void Main(string[] args)
            {
                XmlDocument xmlDocument1 = new XmlDocument();
                xmlDocument1.Load(@"D:\test\xml\1213_test1.xml");
                XmlDocument xmlDocument2 = new XmlDocument();
                xmlDocument2.Load(@"D:\test\xml\1213_test2.xml");
    
                XmlNode xmlNode = xmlDocument1.SelectSingleNode("/root/Broker[@ID='4E']");
                XmlNode xmlNode2 = xmlDocument2.SelectSingleNode("/root/Broker[@ID='4E']");
                if (xmlNode!=null)
                {
                    XmlNode parentNode = xmlNode.ParentNode;
                    parentNode.RemoveChild(xmlNode);
                    if (xmlNode2!=null)
                    {
                        XmlNode importNode = parentNode.OwnerDocument.ImportNode(xmlNode2, true);
                        parentNode.AppendChild(importNode);
                    }
                    xmlDocument1.Save(@"D:\test\xml\1213_test1.xml");
                }
            }

    The xml before modification:

    <?xml version="1.0" encoding="utf-8" ?>
    <root>
      <Broker ID="4E" Ticker_Id="ADBE" BrokerCategory="Contributing" Client="">
        <Broker_Id>0</Broker_Id>
        <ALLTabsUnderBroker>
          <Broker>4E</Broker>
          <TAB>File1</TAB>
          <Commnet1 />
          <Commnet2 />
          <Broker_Id>0</Broker_Id>
          <ReviewedEarnings />
        </ALLTabsUnderBroker>
      </Broker>
    </root>

    The xml after modification:

    <?xml version="1.0" encoding="utf-8"?>
    <root>
     <Broker ID="4E" Ticker_Id="ADBE" BrokerCategory="Contributing" Client="">
        <Broker_Id>0</Broker_Id>
        <ALLTabsUnderBroker>
          <Broker>4E</Broker>
          <TAB>File2</TAB>
          <Commnet1 />
          <Commnet2 />
          <Broker_Id>0</Broker_Id>
          <ReviewedEarnings />
        </ALLTabsUnderBroker>
      </Broker>
    </root>
    

    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.



    Friday, December 13, 2019 6:55 AM

All replies

  • You have the basic idea, I think, but there are some details.  One thing to notice is the <Broker_Id> record in there; it's not a pure list of ALLTabsUnderBrokers tags, so the Union/Intersection thing isn't quite right.

    However, you can extract the ALLTabsUnderBrokers tags from the two lists and manipulate them.  For example:

        var xml1 = XElement.Load("file1.xml");
        IEnumerable<XElement> xml1recs = xml1.Elements("ALLTabsUnderBroker")
        var xml2 = XElement.Load("file2.xml");
        IEnumerable<XElement> xml2recs = xml2.Elements("ALLTabsUnderBroker");

    Now, xml1recs and xml2recs are enumerable lists of just those records.  So, at that point, you should be able to do

        var unique = xml1recs.Union(xml2recs);

    but I couldn't get that to work in a few minutes.  Even if I supply an IEqualityComparer, it doesn't get called.  But that's the right direction.

    When you reconstruct the document, you'd need to re-add the <Broer_Id> tag.


    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.


    • Edited by Tim Roberts Friday, December 13, 2019 1:04 AM
    • Marked as answer by Sudip_inn Friday, December 13, 2019 6:55 AM
    Friday, December 13, 2019 1:04 AM
  • Hi Sudip_inn,

    Thank you for posting here.

    I made some modifications based on the xml content you gave, and this is a code example.

            tatic void Main(string[] args)
            {
                XmlDocument xmlDocument1 = new XmlDocument();
                xmlDocument1.Load(@"D:\test\xml\1213_test1.xml");
                XmlDocument xmlDocument2 = new XmlDocument();
                xmlDocument2.Load(@"D:\test\xml\1213_test2.xml");
    
                XmlNode xmlNode = xmlDocument1.SelectSingleNode("/root/Broker[@ID='4E']");
                XmlNode xmlNode2 = xmlDocument2.SelectSingleNode("/root/Broker[@ID='4E']");
                if (xmlNode!=null)
                {
                    XmlNode parentNode = xmlNode.ParentNode;
                    parentNode.RemoveChild(xmlNode);
                    if (xmlNode2!=null)
                    {
                        XmlNode importNode = parentNode.OwnerDocument.ImportNode(xmlNode2, true);
                        parentNode.AppendChild(importNode);
                    }
                    xmlDocument1.Save(@"D:\test\xml\1213_test1.xml");
                }
            }

    The xml before modification:

    <?xml version="1.0" encoding="utf-8" ?>
    <root>
      <Broker ID="4E" Ticker_Id="ADBE" BrokerCategory="Contributing" Client="">
        <Broker_Id>0</Broker_Id>
        <ALLTabsUnderBroker>
          <Broker>4E</Broker>
          <TAB>File1</TAB>
          <Commnet1 />
          <Commnet2 />
          <Broker_Id>0</Broker_Id>
          <ReviewedEarnings />
        </ALLTabsUnderBroker>
      </Broker>
    </root>

    The xml after modification:

    <?xml version="1.0" encoding="utf-8"?>
    <root>
     <Broker ID="4E" Ticker_Id="ADBE" BrokerCategory="Contributing" Client="">
        <Broker_Id>0</Broker_Id>
        <ALLTabsUnderBroker>
          <Broker>4E</Broker>
          <TAB>File2</TAB>
          <Commnet1 />
          <Commnet2 />
          <Broker_Id>0</Broker_Id>
          <ReviewedEarnings />
        </ALLTabsUnderBroker>
      </Broker>
    </root>
    

    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.



    Friday, December 13, 2019 6:55 AM
  • @Timon thanks for reply and sample code.

    1) please guide me how to do the same using xdocument & xelement class instead of XmlDocument

    2) if i use XmlDocument class and if i need to mention more parameter in SelectSingleNode() function then how can i do it ?

    XmlNode xmlNode = xmlDocument1.SelectSingleNode("/root/Broker[@ID='4E']");

    if i need to mention ID and Ticker_ID then how to do it in SelectSingleNode() ?

    3) how to change broker id value after fetching with SelectSingleNode ?

    XmlNode xmlNode2 = xmlDocument2.SelectSingleNode("/root/Broker[@ID='4E']");

    please answer for my above 3 questions.

    thanks



    • Edited by Sudip_inn Friday, December 13, 2019 9:11 PM
    Friday, December 13, 2019 8:25 PM