将XML转换为字符串时出错

将XML转换为字符串时出错,xml,vb.net,string,xslt,converter,Xml,Vb.net,String,Xslt,Converter,我试图将XmlDocument转换为字符串,以便在转换中使用,但路径中出现了非法字符。异常 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'

我试图将
XmlDocument
转换为字符串,以便在转换中使用,但路径中出现了
非法字符。
异常

    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:


-->一些元素和东西-不相关
5.
-->一些元素和东西
5.
-->一些元素和东西
6.
-->一些元素和东西
6.
-->一些元素和东西
-->结构相同,但位置7和位置8重复数次。
XSLT:


但我使用它是因为我从硬编码字符串中获取它,但这不是问题所在,因为加载运行平稳。无论如何,以下是如何将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
对象是的一个实例。如果是这样,那么您需要使用不同的方法重载。所有带有
String
参数的版本都希望输入文档有一个字符串,因此您不想使用其中任何一个。在转换之前,您需要使用该方法将从数据库检索到的XML字符串加载到。然后您将能够使用该方法的其他重载之一,如下所示

Dim xmlString As String=“…”来自数据库的XML字符串
Dim xmlIn作为新的XmlDocument()
LoadXml(xmlString)

Dim xslString As String=“我做了非常类似的事情(不同的解决方案,多次)在转换方法上,我总是遇到这样一个例外:
不能从已经加载的输入文档中删除空白。请将输入文档作为XmlReader提供。
如果我将
XmlReader
的对象而不是
.Transform()的第一个参数
然后转换不起作用-结果是我得到了一个空文档。我已经完全没有想法了。好的,我添加了XML和XSLT代码。我用XmlPad分别测试了它,转换成功了。:)我能够重现
空白不能被剥离的问题。
但是使用
XmlReader
对我来说工作正常,即使像您发布的那样将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>
"<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>"