0

我正在尝试将 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>"
4

1 回答 1

0

我假设该xslt对象是XslCompiledTransform. 如果是这样,那么您需要使用该Transform()方法的不同重载。所有带String参数的版本都需要输入文档的URI,因此您不想使用其中任何一个。您需要先XmlDocument使用该LoadXml()方法将从数据库检索到的 XML 字符串加载到一个方法中,然后才能对其进行转换。然后您将能够使用该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 回答