簡體   English   中英

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

如何將這個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>

好吧,借助這篇文章,我設法做到了:

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM