how to format xml string with an xsl stylesheet

Hi, I have a string that is in an xml format <items><num>5</num><text>sometext</text></items>

I want to transform that string somewhow using my xsl document listed below and attach it to the body of an email. I was trying to use the code below but the xmldocument load does not take a string. How can i go about accomplishing this.

XmlDocument docXml = new XmlDocument();

docXml.Load(sb.ToString());

XslCompiledTransform docXsl = new XslCompiledTransform();

docXsl.Load("xslFormats.xsl");

Thanks!

Alex



Answer this question

how to format xml string with an xsl stylesheet

  • szucconi

    Could it be that i should not be loading the original string into the body of the email as follows:

    msgMail.Body = sb.ToString();

    should i be adding something else into the body as opposed to the original string that i thought gets formatted with the xml and xsl loads


  • SamSL

    I don't think you're doing a transform at all.

    I don't see a line which calls the Transform method on the XSLT class.

     And this line:

    msgMail.Body = sb.ToString();

    absolutely loads your initial XML into the mail message body.

     



  • Xenocrates

    This is what i am trying to accomplish, and im hoping somebody could help me cause it still aint working. I have an string that contains xml formatted contents. I am trying to take that string, format it in a better way (i was thinking an xsl stylesheet would do) and put it into the body of an email to send back to somebody. The code i am using is as follows:

    XmlDocument docXml = new XmlDocument();

    docXml.LoadXml(sb.ToString());

    XslCompiledTransform docXsl = new XslCompiledTransform();

    docXsl.Load(Server.MapPath("purchaseorder.xsl"));

    MailMessage msgMail = new MailMessage();

    msgMail.To.Add(new MailAddress("info@cleanalliance.com"));

    msgMail.From = new MailAddress("alex@cleanalliance.com");

    msgMail.IsBodyHtml = true;

    msgMail.Subject = "G&K Purchase Order";

    msgMail.Body = sb.ToString();

    SmtpClient client = new SmtpClient();

    client.Send(msgMail);

    When i get the email back now it looks like this, i need to format it in a better looking way :

    5555501CL102262/22/2006 12:00:00 AM5555501CL108882/22/2006 12:00:00 AM5555501CL119082/22/2006 12:00:00 AM


  • casimir

    OK, it sounds like you need to work on your XSL then



  • mirzahat

    You need to output the transformed XML to a source that you can pass to your mail message.



  • Roberto Brunetti

    And thanks for all your replies and help! I've been busting my head trying to figure this out for a while.

    Thanks so much!


  • Dean Perry

    Does everything else seem ok with what i am doing!

    My xml string looks like this:

    {<absorbentitems><item><ponumber>4353</ponumber><dpc>501</dpc><absorbentcode>CL1019</absorbentcode><quantity>6</quantity><orderdate>2/22/2006 12:00:00 AM</orderdate></item><item><ponumber>4353</ponumber><dpc>501</dpc><absorbentcode>CL1088</absorbentcode><quantity>6</quantity><orderdate>2/22/2006 12:00:00 AM</orderdate></item><item><ponumber>4353</ponumber><dpc>501</dpc><absorbentcode>CL1190</absorbentcode><quantity>8</quantity><orderdate>2/22/2006 12:00:00 AM</orderdate></item></absorbentitems>}

    My xsl file looks like so:

    < xml version="1.0" encoding="utf-8" >

    <xsl:stylesheet version="1.0"

    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html" indent="yes"/>

    <!-- item -->

    <xsl:template match="item">

    <xsl:apply-templates select="ponumber" />

    <br></br>

    <xsl:apply-templates select="dpc" />

    <br></br>

    <xsl:apply-templates select="absorbentcode" />

    <br></br>

    <xsl:apply-templates select="quantity" />

    <br></br>

    <xsl:apply-templates select="orderdate" />

    <br></br>

    </xsl:template>

    </xsl:stylesheet>


  • Emil Christopher Melar

    I 'm not sure what you are trying to accomplisch but if you wish to load a (xml)string in a XmlDocument you can use the LoadXml method.

    XmlDocument docXml = new XmlDocument();

    docXml.LoadXml(textBox1.Text);



  • Lord Finn

    Use the LoadXML method to load XML from a string.



  • Ross Grayum Microsoft

    Thanks a million that was a HUGE help!

    Alex Zaff
    alex@cleanalliance.com
    Environmental Services


  • Richard Hafner

    Ok now i am doing a transform on the xslt as so:

    docXsl.Transform(docXml,null,Response.Output);

    But then what would i use to display the new formatted text into the body of my email:

    msgMail.Body = docXml........

    Thanks!


  • how to format xml string with an xsl stylesheet