Solved

Getting XML as a String and displaying it in XSL?

Posted on 2004-07-30
6
673 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
Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

 

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

821 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