Solved

Getting XML as a String and displaying it in XSL?

Posted on 2004-07-30
6
663 Views
Last Modified: 2012-05-05
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
0
Comment
Question by:nryan30
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:gregasm
ID: 11683029
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;
               }
          }
     }

0
 
LVL 8

Expert Comment

by:gregasm
ID: 11683038
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.
0
 
LVL 2

Expert Comment

by:mahanatti
ID: 11725143
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.
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:nryan30
ID: 11734900
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>";

0
 
LVL 8

Expert Comment

by:gregasm
ID: 11736274
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)
0
 
LVL 2

Accepted Solution

by:
mahanatti earned 250 total points
ID: 11749827
In case ur XML is a string, the following could be used.

XmlDocument objDocument = new XmlDocument();
string xmlString = "<XML><test>hello</test></XML>";
objDocument.LoadXml(xmlString);

Xmldocument class has to methods to load Xml data into the instance, namely, Load() and LoadXml(). LoadXml() method can be used to load the Xml that is available as a string.

-Maha
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now