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