Solved

Getting XML as a String and displaying it in XSL?

Posted on 2004-07-30
6
678 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

 

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

729 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