venerdì, luglio 01, 2005

[c#]classe per trasformazioni xml/xsl


ecco una classe che restituisce un documento xml formattato secondo un foglio di stile XML

/// <summary>
/// La classe TrasformXML fornisce 4 metodi che permettono di effettuare una trasformazione XSLT
/// i metodi ritornano una stringa XML valida oppure una stringa di errore
/// i 4 metodi servono per effettuare trasformazioni da stringhe xml oppure da file
/// ritorna molto utile nel caso l'XML venga generato runtime (per esempio pescando i dati da un DB)
/// oppure quando si hanno i fogli di stile salvati in un DB , cosi' non occorre salvare un file temporaneo
/// </summary>
sealed public class TrasformXML
{
//La classe non ha costruttore poiche' non serve Istanziarla per usarla
//in maniera simile ai metodi della classe Math
public string fromXmlUri(string xmluri,string xsl)
{

try
{

XslTransform xt = new XslTransform();

System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml(xsl);

xt.Load(doc.CreateNavigator());

XPathDocument xp = new XPathDocument(xmluri);

StringBuilder message =new StringBuilder();
xt.Transform(xp,null,new StringWriter(message),null);

return message.ToString();
}
catch(Exception er)
{
return er.ToString();
}

}

public string fromXslUri(string xml,string xsluri)
{
System.Xml.Xsl.XslTransform xt = new XslTransform();

ASCIIEncoding asc = new ASCIIEncoding();
byte[] firstString = asc.GetBytes(xml);

MemoryStream st = new MemoryStream(firstString.Length);
st.Write(firstString,0,firstString.Length);
st.Seek(0, SeekOrigin.Begin);

System.Xml.XPath.XPathDocument xp = new XPathDocument(st);
try
{

xt.Load(xsluri);
StringBuilder message =new StringBuilder();
xt.Transform(xp,null,new StringWriter(message),null);

return message.ToString();

}
catch(Exception er)
{
return er.ToString();

}
finally
{
//Libero le risorse
xp=null;
st=null;
firstString=null;
asc=null;
xt=null;
}


}
public string fromUrl(string xmluri,string xsluri)
{
try
{
XslTransform xt = new XslTransform();
xt.Load(xsluri);
XPathDocument xp = new XPathDocument(xmluri);
StringBuilder message =new StringBuilder();
xt.Transform(xp,null,new StringWriter(message),null);
return message.ToString();
}
catch(Exception er)
{
return er.ToString();

}

}

public string fromStr(string xml, string xsl)
{

System.Xml.Xsl.XslTransform xt = new XslTransform();
ASCIIEncoding asc = new ASCIIEncoding();
byte[] firstString = asc.GetBytes(xml);
MemoryStream st = new MemoryStream(firstString.Length);
st.Write(firstString,0,firstString.Length);
st.Seek(0, SeekOrigin.Begin);
System.Xml.XPath.XPathDocument xp = new XPathDocument(st);

try
{
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();

doc.LoadXml(xsl);
xt.Load(doc.CreateNavigator());
StringBuilder message =new StringBuilder();
xt.Transform(xp,null,new StringWriter(message),null);
return message.ToString();
}
catch(Exception er)
{
return er.ToString();

}
finally
{
//Libero le risorse
st=null;
xp=null;
firstString=null;
asc=null;
xt=null;

}

}
}

Nessun commento: