Link to home
Start Free TrialLog in
Avatar of sonicysa
sonicysa

asked on

xmlstream into treeview or xmldatasource

I have the following code and I can use a URL for the xmldatasource if I put the following code into a response.write mode and then put it on an external server. I want to know though how to pass the xml into the data property of the xmldatasource control.

  DataSet ds = new DataSet("Test");
        ds.Load(cmd.ExecuteReader(), LoadOption.OverwriteChanges, "Test");

        XslCompiledTransform xsltvar = new XslCompiledTransform();
        string xslFilePath = Request.MapPath("~/test.xsl", Request.ApplicationPath, false);
        xsltvar.Load(xslFilePath);

        StringReader sr = new StringReader(ds.GetXml());
        XmlDocument doc = new XmlDocument();
        doc.Load(sr);
        XmlElement root = doc.DocumentElement;
        XPathNavigator xPath = root.CreateNavigator();

        //XmlTextWriter writer = new XmlTextWriter("books.html", null);
        MemoryStream mystream = new MemoryStream();
        XmlTextWriter writer = new XmlTextWriter(mystream, null);

        //StreamWriter mystreamWriter = new StreamWriter(mystream);

        //Response.ContentType = "text/xml";
        xsltvar.Transform(xPath, null, writer);
        XmlTextReader reader = new XmlTextReader(mystream);
        //StreamReader mystreamReader = new StreamReader(mystream);
       
//here there must be a way to do it but I don't understand all the different stream manipulations really.
XmlDataSource1.Data = reader.ToString();
        XmlDataSource1.DataFile = null;
        XmlDataSource1.TransformFile = null;
        //TreeView1.DataSource = mystream;
        //XmlDataSource1.DataBind();
        //TreeView1.DataBind();
Avatar of sonicysa
sonicysa

ASKER

This is what ended up working for me:

        string SQLstr = "SELECT * from whatever";
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["connstrInWebConfig"].ConnectionString);
        SqlCommand cmd = new SqlCommand(SQLstr, conn);
        conn.Open();
        DataSet ds = new DataSet("Test");
        ds.Load(cmd.ExecuteReader(), LoadOption.OverwriteChanges, "Test");

        XslCompiledTransform xsltvar = new XslCompiledTransform();
        string xslFilePath = Request.MapPath("~/test.xsl", Request.ApplicationPath, false);
        xsltvar.Load(xslFilePath);

        StringReader sr = new StringReader(ds.GetXml());
        XmlDocument doc = new XmlDocument();
        doc.Load(sr);
        XmlElement root = doc.DocumentElement;
        XPathNavigator xPath = root.CreateNavigator();
       
        StringWriter strwriter = new StringWriter();
        xsltvar.Transform(xPath,null,strwriter);
        string strOutput = strwriter.ToString();
       
        XmlDataSource1.Data = strOutput;
        XmlDataSource1.DataFile = "";
        XmlDataSource1.TransformFile = "";        
        XmlDataSource1.DataBind();
ASKER CERTIFIED SOLUTION
Avatar of kodiakbear
kodiakbear

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial