0

I have two xml files. One is original file and second xml file contains new updates to be made in original xml.

Original xml

  <SPaper>
    <PF>
      <CC>2</CC>
      <Amount>
        <Column1>AmountItem</Column1>
        <Column2>WithTax</Column2>
      </Amount>
      <Amount1>
        <Column1>Amt</ColumnDataField>
        <Text>Declaration</Text>
      </Amount1>
   </PF>
</SPaper>

My new xml contains only that part which should be updated in original xml

 <SPaper>
    <PF>
      <CC>2</CC>
      <Amount>
        <Column1>UpdateAmountItem</Column1>
        <Column3>NewElemnet</Column3>
      </Amount>
      <Amount2>
        <Column1>Amt</ColumnDataField>
        <Text>Declaration</Text>
      </Amount2>
   </PF>
</SPaper>

So what I need is, I want to update, SPaper->PF->CC->Amount->Column1 Value and I need to add new element <Column3> and new node Amount3 in original xml

So the final xml should look like,

 <SPaper>
    <PF>
      <CC>2</CC>
      <Amount>
        <Column1>UpdateAmountItem</Column1>
        <Column2>WithTax</Column2>
        <Column3>NewElemnet</Column2>
      </Amount>
      <Amount1>
        <Column1>Amt</ColumnDataField>
        <Text>Declaration</Text>
      </Amount1>
      <Amount2>
        <Column1>Amt</ColumnDataField>
        <Text>Declaration</Text>
      </Amount2>
   </PF>
</SPaper>

I have tried below code, but it is not working as expected,

 XElement xFileRoot = XElement.Load(OriginalXmlpath);
 XElement xFileChild = XElement.Load(NewXmlPath);
 xFileRoot.Add(xFileChild);
 xFileRoot.Save("file1.xml");

Your help is really appreciated. thanks

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
Johny Bravo
  • 414
  • 9
  • 34
  • You expected that adding one XML file to another would result in inline updates to individual XML nodes? –  Dec 29 '17 at 17:51
  • Yes kind of. The second xml contains only the nodes which has to be updated or added in original file.But my code obviously fails – Johny Bravo Dec 29 '17 at 17:56
  • `XElement.Add` doesn't know that. it will simply add one file to the end of the other. You have to update individual nodes yourself. –  Dec 29 '17 at 17:58
  • Possible duplicate of [Edit specific Element in XDocument](https://stackoverflow.com/questions/18508765/edit-specific-element-in-xdocument) –  Dec 29 '17 at 17:59
  • Yeah I got the results in that way. So I need parse new xml and whatever I am getting in new will be adding to original? – Johny Bravo Dec 29 '17 at 18:00
  • For each element you want to update, you need to 1) pull the value out of the appropriate node from the other file, then 2) set the element's value. Follow that dupe question for one node. If you run into trouble, update your question with your new code. –  Dec 29 '17 at 18:02
  • I have checked the dup question you are pointing. But it is really hardcoded nodes values. In my case my new xml will contain any 2 nodes (which will be changing often) out of total 500 nodes in the original. Can't really check in this way... – Johny Bravo Dec 29 '17 at 18:14
  • The dupe question uses hardcoded values, but that doesn't matter, because you are pulling values out of the original XML. It doesn't matter where the values came from, hardcoded or not. "my new xml will contain any 2 nodes out of total 500 nodes in the original" What? –  Dec 29 '17 at 18:14
  • What I mean is, my original xml contains 500 nodes and my new xml will contain 2/3/5 nodes which are present in original xml and some addition which are not in original. So I am not sure which node I am updaing – Johny Bravo Dec 29 '17 at 18:18
  • Ok I am trying to dig in.I will let you know what I have tried :) – Johny Bravo Dec 29 '17 at 18:23
  • I'm at work and can't take the time to give you a proper answer. But I can point you in the right direction. Learn some XPath, figure out how to (uniquely) identify (using XPath) individual elements. Then you're 90% there, because that's the hard part. –  Dec 29 '17 at 18:25
  • Can this be done without using xpath – Johny Bravo Dec 30 '17 at 12:47
  • 2
    Have you looked at or tried existing solutions such as [How do I combine two XElements with linq select?](https://stackoverflow.com/q/31676600) or [Merging of xml documents](https://stackoverflow.com/q/7726955) or [Merge two XElements](https://stackoverflow.com/a/5166032) or [What is the fastest way to combine two xml files into one](https://stackoverflow.com/q/982597) or [Merging XML Elements with LINQ](https://stackoverflow.com/q/11727585) or [Using linq to merge multiple XML files with the same structure and removing duplicates based on a key](https://stackoverflow.com/q/29802221)? – dbc Dec 30 '17 at 19:03
  • 1
    Your XML is invalid: `Amt` so maybe that's what the parser chokes on. – Mr Lister Jan 01 '18 at 15:26

0 Answers0