如何在.Net中将复杂XML转换为CSV文件?

时间:2012-02-10 13:27:42

标签: xml xslt csv

我有将复杂XML文档转换为CSV文件的要求,该文件应具有特定格式。我尝试在.Net中使用许多样本并试图以多种模式创建XSL但我无法得到我需要的确切内容。如果任何人都可以给我一些saple XSL文件,通过它我可以实现所需的输出,这将是有用的。在这里,我附加了示例XML和我尝试的XSL文件,最后是我需要的示例输出文件。

示例输入XML

<?xml version="1.0" encoding="UTF-8"?>
<loggingpayload xmlns:e="http://schema.dadcdigital.com/dbb/data/2010/externaltask/logging" xmlns:t="http://schema.dadcdigital.com/dbb/data/2010/externaltask/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
  <t:duedate>12/22/2011 8:00:00 AM</t:duedate>
  <e:workflowtype>Tape</e:workflowtype>
  <e:title xmlns:a="http://schema.dadcdigital.com/dbb/data/2010/inventory/">
    <a:guid>d572c250-b8a6-e011-88b0-e532a29f83eb</a:guid>
    <a:displaycode>237557</a:displaycode>
    <a:titlelevel>Episode</a:titlelevel>
    <a:name>EPISODE #0406</a:name>
    <a:seriesname>UGLY BETTY</a:seriesname>
    <a:seasonname>SEASON 04</a:seasonname>
    <a:episodenumber>0406</a:episodenumber>
    <a:alphas>
      <a:alpha>
        <a:guid>2c7ec7d5-6ca7-e011-88b0-e532a29f83eb</a:guid>
        <a:displaycode>410163</a:displaycode>
        <a:externalalphakey>1100181</a:externalalphakey>
        <a:name>Negative</a:name>
        <a:components>
          <a:component type="Audio Track">
            <a:guid>8ccf3c1b-e3c1-4ee1-8fca-6b55ed3276e8</a:guid>
            <a:displaycode>129183</a:displaycode>
            <a:componentstatus>Validation</a:componentstatus>
            <a:tapebarcode></a:tapebarcode>
            <a:componentgroupassociations>
              <a:componentgroupassociation>
                <a:componentgroup>
                  <a:type>KitContentGroup</a:type>
                  <a:guid>9fd12a60-5527-e111-b38e-00155d0a22a0</a:guid>
                  <a:displaycode>123884</a:displaycode>
                </a:componentgroup>
                <a:offset>0</a:offset>
              </a:componentgroupassociation>
            </a:componentgroupassociations>
            <a:audioconfiguration>5.1</a:audioconfiguration>
            <a:audiocontent></a:audiocontent>
            <a:language></a:language>
            <a:audiomix></a:audiomix>
            <a:runtime></a:runtime>
            <a:audiocodec>PCM</a:audiocodec>
            <a:audiocontainer>MXF</a:audiocontainer>
            <a:sampleratehz>48000Hz</a:sampleratehz>
            <a:bitspersample>24</a:bitspersample>
            <a:audiobitratekbps>1125</a:audiobitratekbps>
            <a:isconstantbitrate>True</a:isconstantbitrate>
            <a:minimumbitratekbps></a:minimumbitratekbps>
            <a:maximumbitratekbps></a:maximumbitratekbps>
            <a:audiochannelcomponents>
              <a:audiochannelcomponent>
                <a:guid>533752b4-2ec2-492c-a0f4-180f31736989</a:guid>
                <a:displaycode>129186</a:displaycode>
                <a:componentstatus>Validation</a:componentstatus>
                <a:filedescriptors>
                  <a:filedescriptor>
                    <a:guid>2b2cf10d-5b27-e111-b38e-00155d0a22a0</a:guid>
                    <a:encodingequipment></a:encodingequipment>
                    <a:encodingprofile></a:encodingprofile>
                    <a:checksumvalue></a:checksumvalue>
                    <a:checksumtype></a:checksumtype>
                    <a:fileformat>audio/wav;.wav</a:fileformat>
                    <a:encodedate></a:encodedate>
                  </a:filedescriptor>
                </a:filedescriptors>
                <a:audiochannelinfile>1</a:audiochannelinfile>
                <a:audiochannelontape></a:audiochannelontape>
                <a:audiostreaminfile>1</a:audiostreaminfile>
                <a:audiochannelassignment>Rear Left</a:audiochannelassignment>
              </a:audiochannelcomponent>
              <a:audiochannelcomponent>
                <a:guid>33a98bbc-e973-45aa-996b-8098e5dc4ef0</a:guid>
                <a:displaycode>129187</a:displaycode>
                <a:componentstatus>Validation</a:componentstatus>
                <a:filedescriptors>
                  <a:filedescriptor>
                    <a:guid>2e2cf10d-5b27-e111-b38e-00155d0a22a0</a:guid>
                    <a:encodingequipment></a:encodingequipment>
                    <a:encodingprofile></a:encodingprofile>
                    <a:checksumvalue></a:checksumvalue>
                    <a:checksumtype></a:checksumtype>
                    <a:fileformat>audio/wav;.wav</a:fileformat>
                    <a:encodedate></a:encodedate>
                  </a:filedescriptor>
                </a:filedescriptors>
                <a:audiochannelinfile>1</a:audiochannelinfile>
                <a:audiochannelontape></a:audiochannelontape>
                <a:audiostreaminfile>1</a:audiostreaminfile>
                <a:audiochannelassignment>Rear Right</a:audiochannelassignment>
              </a:audiochannelcomponent>
              <a:audiochannelcomponent>
                <a:guid>a641195e-b8c2-4b22-b0db-8b78d6b3bd4f</a:guid>
                <a:displaycode>129188</a:displaycode>
                <a:componentstatus>Validation</a:componentstatus>
                <a:filedescriptors>
                  <a:filedescriptor>
                    <a:guid>52ba6307-5b27-e111-b38e-00155d0a22a0</a:guid>
                    <a:encodingequipment></a:encodingequipment>
                    <a:encodingprofile></a:encodingprofile>
                    <a:checksumvalue></a:checksumvalue>
                    <a:checksumtype></a:checksumtype>
                    <a:fileformat>audio/wav;.wav</a:fileformat>
                    <a:encodedate></a:encodedate>
                  </a:filedescriptor>
                </a:filedescriptors>
                <a:audiochannelinfile>1</a:audiochannelinfile>
                <a:audiochannelontape></a:audiochannelontape>
                <a:audiostreaminfile>1</a:audiostreaminfile>
                <a:audiochannelassignment>LFE</a:audiochannelassignment>
              </a:audiochannelcomponent>
              <a:audiochannelcomponent>
                <a:guid>5a151669-7731-4ef7-87b3-8e77a32acae1</a:guid>
                <a:displaycode>129189</a:displaycode>
                <a:componentstatus>Validation</a:componentstatus>
                <a:filedescriptors>
                  <a:filedescriptor>
                    <a:guid>4fba6307-5b27-e111-b38e-00155d0a22a0</a:guid>
                    <a:encodingequipment></a:encodingequipment>
                    <a:encodingprofile></a:encodingprofile>
                    <a:checksumvalue></a:checksumvalue>
                    <a:checksumtype></a:checksumtype>
                    <a:fileformat>audio/wav;.wav</a:fileformat>
                    <a:encodedate></a:encodedate>
                  </a:filedescriptor>
                </a:filedescriptors>
                <a:audiochannelinfile>1</a:audiochannelinfile>
                <a:audiochannelontape></a:audiochannelontape>
                <a:audiostreaminfile>1</a:audiostreaminfile>
                <a:audiochannelassignment>Front Center</a:audiochannelassignment>
              </a:audiochannelcomponent>
              <a:audiochannelcomponent>
                <a:guid>65476bb6-e997-494e-ace5-bbd1880732a6</a:guid>
                <a:displaycode>129190</a:displaycode>
                <a:componentstatus>Validation</a:componentstatus>
                <a:filedescriptors>
                  <a:filedescriptor>
                    <a:guid>4cba6307-5b27-e111-b38e-00155d0a22a0</a:guid>
                    <a:encodingequipment></a:encodingequipment>
                    <a:encodingprofile></a:encodingprofile>
                    <a:checksumvalue></a:checksumvalue>
                    <a:checksumtype></a:checksumtype>
                    <a:fileformat>audio/wav;.wav</a:fileformat>
                    <a:encodedate></a:encodedate>
                  </a:filedescriptor>
                </a:filedescriptors>
                <a:audiochannelinfile>1</a:audiochannelinfile>
                <a:audiochannelontape></a:audiochannelontape>
                <a:audiostreaminfile>1</a:audiostreaminfile>
                <a:audiochannelassignment>Right</a:audiochannelassignment>
              </a:audiochannelcomponent>
              <a:audiochannelcomponent>
                <a:guid>0d7021a9-151f-425b-b02f-daf65ed72874</a:guid>
                <a:displaycode>129191</a:displaycode>
                <a:componentstatus>Validation</a:componentstatus>
                <a:filedescriptors>
                  <a:filedescriptor>
                    <a:guid>3c5742fc-5a27-e111-b38e-00155d0a22a0</a:guid>
                    <a:encodingequipment></a:encodingequipment>
                    <a:encodingprofile></a:encodingprofile>
                    <a:checksumvalue></a:checksumvalue>
                    <a:checksumtype></a:checksumtype>
                    <a:fileformat>audio/wav;.wav</a:fileformat>
                    <a:encodedate></a:encodedate>
                  </a:filedescriptor>
                </a:filedescriptors>
                <a:audiochannelinfile>1</a:audiochannelinfile>
                <a:audiochannelontape></a:audiochannelontape>
                <a:audiostreaminfile>1</a:audiostreaminfile>
                <a:audiochannelassignment>Left</a:audiochannelassignment>
              </a:audiochannelcomponent>
            </a:audiochannelcomponents>
          </a:component>
        </a:components>
      </a:alpha>
    </a:alphas>
  </e:title>
</loggingpayload>

示例XSL文件

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns:t="http://schema.dadcdigital.com/dbb/data/2010/externaltask/"
                xmlns:e="http://schema.dadcdigital.com/dbb/data/2010/externaltask/logging"
                xmlns:a="http://schema.dadcdigital.com/dbb/data/2010/inventory/">
  <xsl:output method="text" encoding="iso-8859-1"/>
  <!--<xsl:preserve-space elements="*" />-->
  <!--<xsl:template match="loggingpayload">
    <xsl:apply-templates select="e:title"/>
    <xsl:apply-templates select="a:alphas"/>
  </xsl:template>-->

  <xsl:template match="/">
    <xsl:apply-templates select="loggingpayload/e:title"/>
  </xsl:template>

  <xsl:template match="e:title">
    <xsl:value-of select="name()"/>
    <!--<xsl:apply-templates select="output[contains(text(), '.com')]"/>
    <xsl:apply-templates select="input[contains(text(), '.com')]"/>-->
    <xsl:text>&#x0a;</xsl:text>
  </xsl:template>

  <!--<xsl:template match="output|input">
    <xsl:text>, </xsl:text>
    <xsl:value-of select="text()"/>
    <xsl:if test="@ confirmed='no'">
      <xsl:text> *</xsl:text>
    </xsl:if>
  </xsl:template>-->

  <!--<xsl:template match="e:title">
    <xsl:for-each select="*">

      -->
  <!--It can't be used as the name element has Prefix
      <xsl:value-of select="name()"/>-->
  <!--

      -->
  <!--First break line when it reachhes to Alpas-->
  <!--


      -->
  <!--<xsl:value-of select="name()"/>-->
  <!--
      <xsl:value-of select="node()"/>
      <xsl:if test="position( ) != last( )">
        <xsl:value-of select="','"/>
      </xsl:if>

      <xsl:if test="*">
        <xsl:text>Text Alpha</xsl:text>

        -->
  <!--For Alphas-->
  <!--
        <xsl:for-each select="*">
          <xsl:value-of select="."/>
          <xsl:if test="position( ) != last( )">
            <xsl:value-of select="','"/>
          </xsl:if>
        </xsl:for-each>

      </xsl:if>
    </xsl:for-each>
    <xsl:text>&#10;</xsl:text>
  </xsl:template>-->



  <!--<xsl:for-each select="loggingpayload">
      -->
  <!--<xsl:text>Metadata CSV</xsl:text>
      <xsl:text>   </xsl:text>-->
  <!--

      -->
  <!--<xsl:value-of select="name(.)"/>-->
  <!--
      <xsl:text>Due date</xsl:text>
      <xsl:value-of select="t:duedate"/>
      <xsl:text>   </xsl:text>

      <xsl:text>Workflow Type</xsl:text>
      <xsl:value-of select="e:workflowtype"/>
      <xsl:text>   </xsl:text>
      -->
  <!--<xsl:if test="position() = first()">
        <fo:block linefeed-treatment="preserve">
        </fo:block>
      </xsl:if>-->
  <!--

      <xsl:for-each select="e:title">
        <xsl:text>Title:</xsl:text>
        <xsl:value-of select="a:name"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Title ID:</xsl:text>
        <xsl:value-of select="a:guid"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Display code:</xsl:text>
        <xsl:value-of select="a:displaycode"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Title level:</xsl:text>
        <xsl:value-of select="a:titlelevel"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Seriesname:</xsl:text>
        <xsl:value-of select="a:seriesname"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Season name:</xsl:text>
        <xsl:value-of select="a:seasonname"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Episode number:</xsl:text>
        <xsl:value-of select="a:episodenumber"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Display code:</xsl:text>
        <xsl:value-of select="a:displaycode"/>
        <xsl:text>   </xsl:text>
      </xsl:for-each>

      <xsl:for-each select="e:title/a:alphas/a:alpha">
        <xsl:text>Alpha:</xsl:text>
        <xsl:value-of select="a:name"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Alpha ID:</xsl:text>
        <xsl:value-of select="a:guid"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Display code:</xsl:text>
        <xsl:value-of select="a:displaycode"/>
        <xsl:text>   </xsl:text>

        <xsl:text>External alpha key:</xsl:text>
        <xsl:value-of select="a:externalalphakey"/>
        <xsl:text>   </xsl:text>
      </xsl:for-each>

      <xsl:for-each select="e:title/a:alphas/a:alpha/a:components/a:component">
        <xsl:text>Component type:</xsl:text>
        <xsl:value-of select="@type"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Component ID:</xsl:text>
        <xsl:value-of select="a:guid"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Display code:</xsl:text>
        <xsl:value-of select="a:displaycode"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Component status:</xsl:text>
        <xsl:value-of select="a:componentstatus"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Tape barcode:</xsl:text>
        <xsl:value-of select="a:tapebarcode"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Audio configuration:</xsl:text>
        <xsl:value-of select="a:audioconfiguration"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Audio content:</xsl:text>
        <xsl:value-of select="a:audiocontent"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Language:</xsl:text>
        <xsl:value-of select="a:language"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Audiomix:</xsl:text>
        <xsl:value-of select="a:audiomix"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Runtime:</xsl:text>
        <xsl:value-of select="a:runtime"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Audio codec:</xsl:text>
        <xsl:value-of select="a:audiocodec"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Audio container:</xsl:text>
        <xsl:value-of select="a:audiocontainer"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Sampleratehz:</xsl:text>
        <xsl:value-of select="a:sampleratehz"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Bitspersample:</xsl:text>
        <xsl:value-of select="a:bitspersample"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Audio bitrate kbps:</xsl:text>
        <xsl:value-of select="a:audiobitratekbps"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Isconstantbitrate:</xsl:text>
        <xsl:value-of select="a:isconstantbitrate"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Minimum bitrate kbps:</xsl:text>
        <xsl:value-of select="a:minimumbitratekbps"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Maximum bitrate kbps:</xsl:text>
        <xsl:value-of select="a:maximumbitratekbps"/>
        <xsl:text>   </xsl:text>

      </xsl:for-each>

      <xsl:for-each select="e:title/a:alphas/a:alpha/a:components/a:component/a:componentgroupassociations/a:componentgroupassociation/a:componentgroup">
        <xsl:text>Component group type:</xsl:text>
        <xsl:value-of select="a:type"/>
        <xsl:text>   </xsl:text>

        <xsl:text>group id:</xsl:text>
        <xsl:value-of select="a:guid"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Display code:</xsl:text>
        <xsl:value-of select="a:displaycode"/>
        <xsl:text>   </xsl:text>

      </xsl:for-each>

      <xsl:for-each select="e:title/a:alphas/a:alpha/a:components/a:component/a:audiochannelcomponents/a:audiochannelcomponent">        

        <xsl:text>Audio channel component id:</xsl:text>
        <xsl:value-of select="a:guid"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Display code:</xsl:text>
        <xsl:value-of select="a:displaycode"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Status:</xsl:text>
        <xsl:value-of select="a:componentstatus"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Audio channel in file: </xsl:text>
        <xsl:value-of select="a:audiochannelinfile"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Audio channel on tape:</xsl:text>
        <xsl:value-of select="a:audiochannelontape"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Audio stream in file: </xsl:text>
        <xsl:value-of select="a:audiostreaminfile"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Audio channel assignment:</xsl:text>
        <xsl:value-of select="a:audiochannelassignment"/>
        <xsl:text>   </xsl:text>

        <xsl:text>File descriptor ID:</xsl:text>
        <xsl:value-of select="a:filedescriptors/a:filedescriptor/a:guid"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Encoding equipment:</xsl:text>
        <xsl:value-of select="a:filedescriptors/a:filedescriptor/a:encodingquipment"/>
        <xsl:text>   </xsl:text>
        <xsl:text>group id:</xsl:text>
        <xsl:value-of select="a:guid"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Encoding profile:</xsl:text>
        <xsl:value-of select="a:filedescriptors/a:filedescriptor/a:encodingprofile"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Checksum value:</xsl:text>
        <xsl:value-of select="a:filedescriptors/a:filedescriptor/a:checksumvalue"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Checksum type:</xsl:text>
        <xsl:value-of select="a:filedescriptors/a:filedescriptor/a:checksumtype"/>
        <xsl:text>   </xsl:text>

        <xsl:text>File format:</xsl:text>
        <xsl:value-of select="a:filedescriptors/a:filedescriptor/a:fileformat"/>
        <xsl:text>   </xsl:text>

        <xsl:text>Encode date:</xsl:text>
        <xsl:value-of select="a:filedescriptors/a:filedescriptor/a:encodedate"/>
        <xsl:text>   </xsl:text>
      </xsl:for-each>
      -->
  <!--<xsl:if test="position() != last()">
        <xsl:value-of select="normalize-space(.)"/>
        <xsl:text>&#xD;</xsl:text>
      </xsl:if>
      <xsl:if test="position()  = last()">
        <xsl:value-of select="normalize-space(.)"/>
        <xsl:text>&#xD;</xsl:text>
      </xsl:if>-->
  <!--
    </xsl:for-each>
  </xsl:template>-->
</xsl:stylesheet>

示例输出CSV文件在任何文本板中打开时应该看起来像以下。

Due date,Workflow type,,,,,,,,,,,,,,,,
1/25/2012 6:30:00 PM,Tape,,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,
Title,Guid,Display code,Title level,Series name,Season name,Episode number,,,,,,,,,,,
FATHER IS A BACHELOR,3c93ce83-86a6-e011-88b0-e532a29f83eb,153838,NonEpisodic,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,
Alpha Name,Guid,Display code,External alpha key,,,,,,,,,,,,,,
TH,56d1b093-54a7-e011-88b0-e532a29f83eb,293075,964992,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,
Component type,Guid,Display code,Component status,Tape barcode,Audio configuration,audio content,Language,Audiomix,Runtime,Audio codec,Audio container,Sampleratehz,Bitspersample,Audio bitratekbps,Isconstantbitrate,Minimum bitratekbps,Maximum bitratekbps
Audio Track,a9ea97bf-5ce9-4a0d-ad13-26fdeedf24cc,149371,Validation,,LT/RT,Composite,French - Continental,,0:0,PCM,MXF,48000Hz,24,1125,True,0,0
,,,,,,,,,,,,,,,,,
Component group type,Guid,Displaycode,Offset,,,,,,,,,,,,,,
KitContentGroup,db8d8548-8442-e111-8089-00155d0a22a0,132168,,,,,,,,,,,,,,,
,,,,,,,,,,,,,,,,,
Audio Channel Components,,,,,,,,,,,,,,,,,
,Guid,Displaycode,Component status,Audio channel in file,Audio channel on tape,Audio stream in file,Audio channel assignment,File descriptor guid,Encoding equipment,Encoding profile,Checksum value,Checksum type,File format,Encode date,,,
,d79b9897-eaa3-4c6d-8a85-23ce2d52e52f,149372,Validation,1,,1,Left Total,b918d9d4-8542-e111-8089-00155d0a22a0,,,,,audio/wav;.wav,,,,
,0dd87a02-3182-4fa0-9f1c-d16fddf23020,149373,Validation,1,,1,Right Total,bf18d9d4-8542-e111-8089-00155d0a22a0,,,,,audio/wav;.wav,,,,

由于 Sundar Rajan

0 个答案:

没有答案