使用xslt或linq合并两个xml文件

时间:2017-02-28 07:51:38

标签: asp.net xml linq xslt merge

XML1

<Form>
  <Caption>formcaption</Caption>
  <Name>formname</Name>
  <Pages>
    <Page>
      <Caption>pagecaption</Caption>     
      <Name>Page1</Name>      
      <Sections>
        <Section>
          <Caption>sectioncaption</Caption>
          <Name>section1</Name>
          <DataItems>
            <DataItem>
              <Caption>test5caption</Caption>
               <Name>test5Name</Name>            
            </DataItem>        
          </DataItems>
        </Section>
      </Sections>
    </Page>
  </Pages>
</Form>

XML2

    <Form ID='form1'>
  <Pages>
    <Page ID='Page1'>
      <Sections>
        <Section ID='section1'>
          <DataItems>
            <DataItem ID='data3' parentID=''>
              <Value>1234</Value>                 
            </DataItem>            
          </DataItems>
        </Section>    
      </Sections>
    </Page>
  </Pages>
</Form>

outputxml

<Form>
  <Caption>formcaption</Caption>
  <Name>formname</Name>
  <Pages>
    <Page>
      <Caption>pagecaption</Caption>     
      <Name>Page1</Name>      
      <Sections>
        <Section>
          <Caption>sectioncaption</Caption>
          <Name>section1</Name>
          <DataItems>
            <DataItem>
              <Caption>test5caption</Caption>
               <Name>test5Name</Name>
               <Value>1234</Value>
            </DataItem>        
          </DataItems>
        </Section>
      </Sections>
    </Page>
  </Pages>
</Form>

我见过xslt样式表但是如何在c#中实现它我无法将其排序。

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/Form">
    <xsl:copy>
      <xsl:apply-templates select="Pages"/>
      <xsl:apply-templates select="document('c:\users\xljoseph3\documents\visual studio 2012\Projects\ConsoleApplication6\ConsoleApplication6\XMLFile2.xml')/*/Pages"/>

    </xsl:copy>
  </xsl:template>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

0 个答案:

没有答案