【问题标题】:Error at converting XML to String将 XML 转换为字符串时出错
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 我做了非常相似的事情(不同的解决方案,多次),我总是在 Transform 方法上遇到这个异常:White space cannot be stripped from input documents that have already been loaded. Provide the input document as an XmlReader instead. 如果我把XmlReader 的对象而不是第一个参数的.Transform() 然后转换不起作用 - 结果我得到一个空文档。我已经完全没有想法了。
    • 好的,我添加了 XML 和 XSLT 代码。不过,我已经用 XmlPad 单独对其进行了测试,并且转换是成功的。 :)
    • 我能够重现 White space cannot be stripped... 问题。但是使用XmlReader 对我来说效果很好,即使像您发布的那样将XSLT 作为硬编码字符串加载也是如此。无论如何,我已经用稍微不同的方法更新了我的答案。您可以尝试一下,如果您仍有问题,请告诉我。
    猜你喜欢
    • 2023-03-13
    • 2014-01-23
    • 2012-07-29
    • 1970-01-01
    • 1970-01-01
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多