Link to home
Start Free TrialLog in
Avatar of nryan30
nryan30

asked on

Getting XML as a String and displaying it in XSL?

Hi, as of right now, I am not sure if I am going to have a XML file at all.
I may only have the xml in the database as a string. What I will probably need to do, is parse through that string of XML and be able to apply XSL to it, or get it and build a dataset and display somehow that way....

If anyone happens to have any experience in this area and can provide a small, hard coded example, that would probably be the easiest way for me to fully see and understand the process.

Thanks
Avatar of gregasm
gregasm

When you say XML as a string, does that mean the XML reserved characters '<' and '>' are converted to their string equivalent, '&lt;' and '&gt;' before they are stored in the database? If so, then the first step would be to convert those string representations back to XML format.

//retrieve the xml
string xml = getData();

//then convert the string to xml
xml = xml.replace("&lt;", "<");
xml = xml.replace("&gt;", ">");

//now you can apply an xsl stylesheet to the xml to transform it using a method such as this one:

       public static string Transform(string xml, string xsltPath)
          {
               try
               {
                    //convert the XML to another XML using a stylesheet...
                    XslTransform xslt = new XslTransform();
                    //XmlReader xmlreader = new XmlTextReader(xsl, XmlNodeType.Element, null);
                    //xslt.Load(xmlreader, null, new System.Security.Policy.Evidence(null));
                    xslt.Load(xsltPath);
                    //System.Security.Policy.Evidence ev = new System.Security.Policy.Evidence();

                    //create the memory stream output objects
                    using (MemoryStream strm = new MemoryStream())
                    {
                         TextWriter textwriter = new StreamWriter(strm);
                         XmlTextWriter writer = new XmlTextWriter(textwriter);

                         //create the nav
                         //XmlDocument xmlDoc = new XmlDocument();
                         //xmlDoc.LoadXml(xml);
                         XPathDocument xPathDoc = new XPathDocument(new StringReader(xml));

                         xslt.Transform(xPathDoc, null, writer, null);

                         strm.Position = 0;
                         TextReader reader = new StreamReader(strm);
                         string transformedXml = reader.ReadToEnd();
                         reader.Close();
                         return transformedXml;
                    }
               }
               catch (ArgumentNullException ex)
               {
                    throw ex;
               }
               catch (XmlException ex)
               {
                    throw ex;
               }
               catch (IOException ex)
               {
                    throw ex;
               }
               catch (OutOfMemoryException ex)
               {
                    throw ex;
               }
          }
     }

If you are trying to get the xml data into a dataset, then are you serializing the dataset into xml and then persisting it into the database?

You may use DataSet.GetXml() to return the dataset's xml string representation and then store that.

When you retrieve the xml string and read it back into the dataset, you may use DataSet.ReadXml(xml) to load it again.

.NET provides objects that abstract this functionality though. Using the DataAdapter, you may never need to parse the xml manually.

The easiest way to get xml data into a dataset is to use the DataAdapter object's Fill method.
private void fnLoad()
{
               string xmlData = getData();
               xmlData = xmlData.Replace("&lt;", "<");
               xmlData = xmlData.Replace("&gt;", ">");
               XmlDocument objDocument = new XmlDocument();
               objDocument.LoadXml(xmlData);
               string htmlFormat = fnTransform(objDocument);
}

private string fnTransform(XmlDocument xdPatientList)
{
      XslTransform objXSLT = new XslTransform();
      objXSLT.Load(Server.MapPath("ListPatients.xslt"));
            
      MemoryStream xmlMemory = new MemoryStream();
      objXSLT.Transform(xdPatientList, null, xmlMemory);
      return Encoding.UTF8.GetString(xmlMemory.ToArray());
}

If the XmlData is in one of the DataSet columns then take that data into a string like:

string xmlData = objDataSet.Tables[0].Rows[i]["XmlColumn"].ToString();

and then perform the replace code as shown in the code.

If the Xml is in a file, then the XmlDocument passed to teh transform function can be directly loaded from the file like:

objDocument.Load(xmlFilePath);

The above code is in C# and for this to workk you need System.Xml, System.Xml.Xsl, System.IO and  System.Text namespaces.

-Maha.
Avatar of nryan30

ASKER

I have no XML FIle Path.
Say it is a pull fromn the DB, and is stored in a String variable... IE:

String XML = "<XML><test>hello</test></XML>";

To achieve the same results, in the end we need to load the Xml into a stream.

string XML = "<XML><test>hello</test></XML>";
objDocument.Load(new System.IO.StringReader(XML));

That is another means to the same ends, my friend.

(the "my friend" part just seemed to be the proper end to that last sentence)
ASKER CERTIFIED SOLUTION
Avatar of mahanatti
mahanatti

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