Solved

Getting XML as a String and displaying it in XSL?

Posted on 2004-07-30
6
670 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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