0

So I have the XSLT code (it's correct, it's tested separately) and I have it hard-coded as a string (that's a requirement, don't bother asking). Loading the XML, the XSLT and all this stuff is OK.

But when I use the XmlDocument as 1st argument in XmlCompiledTransform.Transform() I get exception about White space handling.

Then I use the XmlReader as 1st argument, and this works, but I get exception as I try to save the transformed file, and the exception is Invalid XML document. The document does not have a root element.

Here is the code:

    Dim xsltTransformerCode As New xsltTransformCode()
    Dim myXmlDoc As New XmlDocument()
    Dim resultXmlDoc As New XmlDocument()
    Dim sr As New StringReader(xsltTransformerCode.transformationXSLTcode())
    Dim xr As XmlReader = XmlReader.Create(sr)
    Dim xsltTransCompiled As New XslCompiledTransform()

    'write the stringified xslt code to file, in order to check its validity manually'
    File.WriteAllText("C:\Users\gk\Desktop\tempXSLTcode.xsl", xsltTransformerCode.transformationXSLTcode())
    'load the xml string taken from the database'
    myXmlDoc.Load("C:\Users\gk\Desktop\XTilbud.xml")
    'load the stylesheet'
    xsltTransCompiled.Load(xr)

    Using xw As XmlWriter = resultXmlDoc.CreateNavigator().AppendChild()
        xsltTransCompiled.Transform(myXmlDoc, Nothing, xw)
        xw.Close()
    End Using

    resultXmlDoc.Save("C:\Users\gk\Desktop\myXMLfile.xml")

    sr.Dispose()
    sr.Close()
    xr.Close()

P.S. I want to transform the original document and pass its value to another xmlDocument and save it. (Or if I can transform and save the same object, then it's ok. I am open for suggestions).

So what I need is somehow to get the value of the reader and save it as XML document or smth like that, I am not sure...

4
  • If you want the transformation result as C:\Users\gk\Desktop\myXMLfile.xml, why are you first trying to create an XmlDocument, why don't you simply use an overload of the Transform method that takes a FileStream or XmlWriter (e.g. Using xw As XmlWriter = XmlWriter.Create("C:\Users\gk\Desktop\myXMLfile.xml", xsltTransCompiled.OutputSettings) xsltTransCompiled.Transform(myXmlDoc, Nothing, xw) End Using)? Commented May 26, 2013 at 14:13
  • Well, my initial purpose is to save the result from the applied XSLT transformation into an XML document (object) that I can pass over to smth, further. The thing I am doing now with .Save() is just because of testing purposes, to check if the transformation was successful. Btw, I tried your way, and now my newly created XML is empty... Commented May 26, 2013 at 21:04
  • We need to see the stylesheet code and an input XML sample. Commented May 27, 2013 at 8:55
  • I think the problem is not in the XML and XSLT, but in the thing that there is nothing being written in the resultXmlDoc and that is why I am getting Invalid XML document. The document does not have a root element, because it is actually empty. Commented May 28, 2013 at 6:26

1 Answer 1

1

Your code is a little bit over-complicated for this situation.

Try THIS ONE.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.