Mentransformasi XML menggunakan XSLT di C#

Kode dibawah ini akan mereturn hasil transformasi file XML yang disebutkan di “file_source” menggunakan XSLT yang disebutkan di “file_style”.

public static string GetXslTransform(
  string file_source,
  string file_style)
{
  string strHtml;

  XslCompiledTransform x = new XslCompiledTransform();
  
  // Load the XML 
  XPathDocument doc = new XPathDocument(file_source);

  // Load the style sheet.
  XslCompiledTransform xslt = new XslCompiledTransform();
  xslt.Load(file_style);
  MemoryStream ms = new MemoryStream();
  XmlTextWriter writer = new XmlTextWriter(ms, Encoding.ASCII);
  StreamReader rd = new StreamReader(ms);
  xslt.Transform(doc, writer);
  ms.Position = 0;
  strHtml = rd.ReadToEnd();
  rd.Close();
  ms.Close();
  return strHtml;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s