【发布时间】:2013-05-23 03:31:43
【问题描述】:
我正在尝试将 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>"
【问题讨论】:
标签: xml vb.net string xslt converter