我正在尝试将 an 转换XmlDocument
为字符串,以便可以在转换中使用,但出现Illegal characters in path.
异常。
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")
所以,你看我尝试了 3 种不同的方法将 XML 文档转换为字符串,每次我得到相同的异常。另一方面,当我将 XMl 文件直接放入.Transform()
方法中时,它完全可以正常工作。像这样:
xslt.Transform("C:\Users\myXmlFile.xml", "C:\Users\newXmlFile.xml")
但我需要它作为字符串对象,因为我实际上是从数据库中获取 XML 作为字符串。这只是测试类。因此,在主程序中,我无法将 XML 文档直接从物理文件加载到.Transform()
方法中。
我测试了将其保存stringifiedXmlDoc
到另一个 XML 文档,以检查一些语法错误,但它与原始文档完全相同。
编辑:添加 XML 和 XSLT 代码
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>
但是我在从硬编码字符串中获取它时使用它,但这不是问题,因为加载运行顺利。无论如何,这就是我将 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>"