0

I am trying to convert an XmlDocument to string so I can use in my transformation, but I get Illegal characters in path. exception.

    Dim loadedXmlDoc As New XmlDocument()
    'load the xml string taken from the database'
    loadedXmlDoc.Load("C:\Users\myXmlFile.xml")
    'Dim stringifiedXmlDoc As String = loadedXmlDoc.OuterXml'
    'Dim stringifiedXmlDoc As String = loadedXmlDoc.InnerText'
    Dim sw As New StringWriter()
    Dim xw As New XmlTextWriter(sw)
    loadedXmlDoc.WriteTo(xw)
    Dim stringifiedXmlDoc As String = sw.ToString()

    'load the stylesheet'
    xslt.Load(xr)
    xslt.Transform(stringifiedXmlDoc, "C:\Users\gk\Desktop\newXTilbud.xml")

So, you see I tried 3 different ways of converting the XML document to string and everytime I get the same exception. On the other hand, when I put the XMl file directly into the .Transform() method, it works completely fine. Like this:

xslt.Transform("C:\Users\myXmlFile.xml", "C:\Users\newXmlFile.xml")

But I need it to be as a string object, because I am actually acquiring the XML from a database as string. This here is just test class. So in the main program I cannot load the XML document directly from the physical file into the .Transform() method.

I tested saving the stringifiedXmlDoc to another XML document, to check for some syntax mistakes, but it is completely the same, as the original one.

EDIT: adding the XML and XSLT codes

XML:

<Main>
 <TB>
    --> some elements and stuff - not relevant
   <City>
     <Area>
       <Position>5</Position>
       <House>

       --> some elements and stuff

       </House>
     </Area>
     <Area>
       <Position>5</Position>
       <Block>

       --> some elements and stuff

       </Block>
     </Area>
     <Area>
       <Position>6</Position>
       <House>

       --> some elements and stuff

       </House>
     </Area>
     <Area>
       <Position>6</Position>
       <Block>

       --> some elements and stuff

       </Block>
     </Area>
   </City>
   <City>

   --> same structure but with several repetitions of Position 7 and 8.

   </City>
 </TB>
</Main>

XSLT:

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

  <xsl:key name="AreaByPosition" match="Area" use="Position"/>

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

  <!-- for the first Area in each Position -->
  <xsl:template match="Area[generate-id() = generate-id(key('AreaByPosition', Position)[1])]">
    <Area>
      <!-- copy in the Position element once only -->
      <xsl:apply-templates select="Position"/>
      <!-- copy in all sub-elements except Position from all matching Areas -->
      <xsl:apply-templates select="key('AreaByPosition', Position)/*[not(self::Position)]"/>
    </Area>
  </xsl:template>

  <!-- ignore all other Area elements -->
  <xsl:template match="Area"/>
</xsl:stylesheet>

but I use it as I acquire it from a hard-coded string, but that is not the problem, because the loading runs smoothly. Anyway, here is how I have the XSLT as string:

"<xsl:stylesheet xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"" version=""1.0"">" &
        "<xsl:strip-space elements=""*""/>" &
        "<xsl:output method=""xml"" indent=""yes""/>" &
        "<xsl:key name=""AreaByPosition"" match=""Area"" use=""Position""/>" &
        "<xsl:template match=""@*|node()"">" &
            "<xsl:copy><xsl:apply-templates select=""@*|node()""/></xsl:copy>" &
          "</xsl:template>" &
            "<!-- for the first Area in each Position -->" &
          "<xsl:template match=""Area[generate-id() = generate-id(key('AreaByPosition', Position)[1])]"">" &
            "<Area>" &
              "<!-- copy in the Position element once only -->" &
              "<xsl:apply-templates select=""Position""/>" &
              "<!-- copy in all sub-elements except Position from all matching Areas -->" &
              "<xsl:apply-templates select=""key('AreaByPosition', Position)/*[not(self::Position)]""/>" &
            "</Area>" &
          "</xsl:template>" &
            "<!-- ignore all other Area elements -->" &
          "<xsl:template match=""Area""/>" &
        "</xsl:stylesheet>"
4

1 に答える 1

0

xsltオブジェクトは のインスタンスであると仮定しますXslCompiledTransform。その場合は、Transform()メソッドの別のオーバーロードを使用する必要があります。Stringパラメーターを持つすべてのバージョンは、入力ドキュメントへのURIを想定しているため、それらのいずれも使用したくありません。変換する前にXmlDocument、メソッドを使用して、データベースから取得した XML 文字列を にロードする必要があります。LoadXml()次に、このように、メソッドの他のオーバーロードの 1 つを使用できるようになりますTransform()...

Dim xmlString As String = "<Main><TB> ... </TB></Main>" 'XML string from DB
Dim xmlIn As New XmlDocument()
xmlIn.LoadXml(xmlString)

Dim xslString As String = "<xsl:styleshe..." 'your XSLT as a string
Dim xmlReader As XmlReader = XmlReader.Create(New StringReader(xslString))
Dim xslt As New XslCompiledTransform
xslt.Load(xmlReader)

Using xmlOut As XmlWriter = New XmlTextWriter("C:\Users\gk\Desktop\newXTilbud.xml", Nothing)
    xslt.Transform(New XmlNodeReader(xmlIn), xmlOut)
End Using
于 2013-05-28T09:01:10.550 に答える