将xml插入/替换为Xdocument的指定节点

时间:2018-08-24 12:32:44

标签: c# xml xml-parsing linq-to-xml

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

2 个答案:

答案 0 :(得分:1)

好的,借助于此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>
";

答案 1 :(得分:0)

将您的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>