Insert/Replace xml 进入Xdocument的指定节点

Insert/Replace xml into the specified node of Xdocument

我有以下 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>

以及我通过 XmlDocument 对象的 SelectSingleNode 方法获得的以下 xml:

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

如何insert/replace这个xml进入XDocument的ScheduleOperation节点得到如下结果:

<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>

好的,借助这个post我已经成功做到了:

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>
";

鉴于您的 xdocument.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>

和这个 t.xslt 文件:

<?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>

您冷使用 xmlstarlet 来插入节点:

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>