Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
644 views
in Technique[技术] by (71.8m points)

c# - White space cannot be stripped from input documents that have already been loaded. Provide the input document as an XmlReader instead

I want to transform a XML document, but having a problem.

My XSLT looks like this:

<?xml version="1.0" encoding="iso-8859-1" ?> 
<xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" /> 
    <xsl:strip-space elements="*" /> 

    <xsl:template match="/">
        <xsl:apply-templates select="address" /> 
    </xsl:template>

    <xsl:template match="address">
        <xsl:value-of select="@street" /> 
        <xsl:value-of select="@housenr" /> 
        <xsl:value-of select="@zipcode" /> 
        <xsl:value-of select="@city" /> 
        <xsl:value-of select="@country" /> 
    </xsl:template>
</xsl:stylesheet>

And the XML document I want to transform looks like this:

<address id="4" type="1" 
    typename="Postadres" 
    street="pak street" 
    housenr="420" 
    zipcode="42000" 
    city="Nill" 
    country="Lahore" 
    kix="" /> 

Here is the code I've written:

public static string Transform(XmlDocument doc, XmlDocument stylesheet)
{
    var transform = new System.Xml.Xsl.XslCompiledTransform();
    XmlDocument domOutput = new XmlDocument();

    stylesheet.PreserveWhitespace = false;
    transform.Load(stylesheet);    // compiled stylesheet

    MemoryStream oStream = new MemoryStream();
    var writer = new System.IO.StringWriter();

    transform.Transform(doc, (XsltArgumentList)null, oStream);

    domOutput.Load(oStream);

    return writer.ToString();
}

The following line throws an exception

transform.Transform(doc, (XsltArgumentList)null, oStream);

Exception message:

White space cannot be stripped from input documents that have already been loaded. Provide the input document as an XmlReader instead.

Can you tell me what I am doing wrong?

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I solved it. Actually "XslCompiledTransform.Transform" takes XmlReader as first Parameter and i was passing XmlDocument in First Paramenter. Here is the code.

public static string Transform(XmlDocument doc, XmlDocument stylesheet)
    {
        try
        {
            System.Xml.Xsl.XslCompiledTransform transform = new System.Xml.Xsl.XslCompiledTransform();
            transform.Load(stylesheet); // compiled stylesheet
            System.IO.StringWriter writer = new System.IO.StringWriter();
            XmlReader xmlReadB = new XmlTextReader(new StringReader(doc.DocumentElement.OuterXml));
            transform.Transform(xmlReadB, null, writer);
            return writer.ToString();
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...