0

I have the following XDocument:

<root>
  <object>
    <objectname>Schedule</objectname>
    <mode>Edit</mode>
    <row>
      <idObject>1</idObject>
      <BeginDate>2018-08-07</BeginDate>
      <EndDate>2018-08-07</EndDate>
      <TotalSum>300.17</TotalSum>
      <ScheduleOperation></ScheduleOperation>
    </row>
  </object>
</root>

And the following xml which I got via SelectSingleNode method of XmlDocument object:

<ScheduleOperation>
   <row>
      <Sum>1000.00</Sum>
      <Date>2017-09-25T00:00:00</Date>        
   </row>
</ScheduleOperation>

How can I insert/replace this xml into ScheduleOperation node of XDocument to get the result as follows:

<root>
  <object>
    <objectname>Schedule</objectname>
    <mode>Edit</mode>
    <row>
      <idObject>1</idObject>
      <BeginDate>2018-08-07</BeginDate>
      <EndDate>2018-08-07</EndDate>
      <TotalSum>300.17</TotalSum>
      <ScheduleOperation>
        <row>
          <Sum>1000.00</Sum>
          <Date>2017-09-25T00:00:00</Date>
        </row>
      </ScheduleOperation>
    </row>
  </object>
</root>
3
  • Learn some XSLT and you don't need to do any C# coding. Commented Aug 24, 2018 at 12:34
  • 2
    @Neil how will extensible style sheets solve this problem? Commented Aug 24, 2018 at 12:39
  • 1
    It will allow him to take 2 XML input files and merge them into 1. Isn't that what he's asking how to do? Commented Aug 24, 2018 at 12:40

2 Answers 2

1

OK, with the help of this post I've managed to do it:

void Main()
{

    var initialXDoc = XDocument.Parse(Xml());   
    var emptyScheduleOperation = initialXDoc.XPathSelectElement("/root/object/row/ScheduleOperation");

    var xDocScheduleOperation = XDocument.Parse(Xml2());
    var scheduleOperation = xDocScheduleOperation.XPathSelectElement("ScheduleOperation");

    emptyScheduleOperation.ReplaceWith(scheduleOperation);
}

// initial xml:
private string Xml() => @"
<root>
  <object>
    <objectname>Schedule</objectname>
    <mode>Edit</mode>
    <row>
      <idObject>1</idObject>
      <BeginDate>2018-08-07</BeginDate>
      <EndDate>2018-08-07</EndDate>
      <TotalSum>300.17</TotalSum>
      <ScheduleOperation></ScheduleOperation>
    </row>
  </object>
</root>
";

// should be inserted into the initial xml
private string Xml2() => @"
<ScheduleOperation>
   <row>
      <Sum>1000.00</Sum>
      <Date>2017-09-25T00:00:00</Date>        
   </row>
</ScheduleOperation>
";
Sign up to request clarification or add additional context in comments.

Comments

0

Given your xdocument.xml as:

<root>
  <object>
    <objectname>Schedule</objectname>
    <mode>Edit</mode>
    <row>
      <idObject>1</idObject>
      <BeginDate>2018-08-07</BeginDate>
      <EndDate>2018-08-07</EndDate>
      <TotalSum>300.17</TotalSum>
      <ScheduleOperation></ScheduleOperation>
    </row>
  </object>
</root>

and this t.xslt file:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <!-- Identity transform -->
  <xsl:template match="@* | node()">
     <xsl:copy>
        <xsl:apply-templates select="@* | node()"/>
          </xsl:copy>
  </xsl:template>
  <xsl:template match="/root/object/row/ScheduleOperation">
     <ScheduleOperation>
       <row>
         <Sum>1000.00</Sum>
         <Date>2017-09-25T00:00:00</Date>
       </row>
     </ScheduleOperation>
  </xsl:template>
</xsl:stylesheet>

You cold use xmlstarlet to get the node inserted:

xmlstarlet tr t.xslt xdocument.xml
<?xml version="1.0"?>
<root>
  <object>
    <objectname>Schedule</objectname>
    <mode>Edit</mode>
    <row>
      <idObject>1</idObject>
      <BeginDate>2018-08-07</BeginDate>
      <EndDate>2018-08-07</EndDate>
      <TotalSum>300.17</TotalSum>
      <ScheduleOperation><row><Sum>1000.00</Sum><Date>2017-09-25T00:00:00</Date></row></ScheduleOperation>
    </row>
  </object>
</root>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.