Posts Tagged ‘Xml’

Object Serialize TO Xml

Two Functions to Serialize and detribalize any objects to and from xml

 

 

private string Serialize(object obj) 

{ 

     if (obj == null) { return null; }

     XmlSerializer s = new XmlSerializer(obj.GetType()); 

     TextWriter w = new StringWriter(); 

     s.Serialize(w, obj); 

     w.Close();

     return w.ToString(); 

}

 

 

public static object Deserialize(Type type, string xml) 

{ 

 

 if (string.IsNullOrEmpty(xml)) 

 { 

     return Activator.CreateInstance(type); 

 } 

 

    XmlSerializer s = new XmlSerializer(type); 

    TextReader tr = new StringReader(xml); 

    return s.Deserialize(tr); 

}

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
 

XslCompiledTransform and xsl Scripts

If you are Upgrading Project from .Net 1 to .Net 2 you Will now know that XslTransform is now deprecated. and it is better to use System.Xml.Xsl.XslCompiledTransform
For more info :  see this .

So instead of

 

XslTransform.Load(XslPath);

You will need to write this code instead

XmlUrlResolver XResolver = new XmlUrlResolver();

XResolver.Credentials = 

System.Net.CredentialCache.DefaultCredentials;

XslTransform.Load(fullPath, 

new XsltSettings(true, true), XResolver);

 

How Ever XslComiledTransform still have some security matters.

I was using Aspose.Pdf dlls www.aspose.com To Generate Pdf file from and Xml Data using and Xsl Sheet that had a kind of C# script to get the date time to show it in the footer of the report

but those scripts generated temp dlls and XslCompiledTransform created them in the Temp Folder of the windows and that generated security Permissions error

So after long search i removed the scripts in the Xsl Sheet and passed the calculated Values as Xsl Parameters

To Pass Xsl Paramter in the Xsl File :

xsl:param name="date" /

To Dispaly Xsl Paramter :

xsl:value-of select="$date" /

To Transform the File Using C#:

 

XsltArgumentList reportArgs = new 

XsltArgumentList();

reportArgs.AddParam("date", string.Empty, 

DateTime.Now.ToString());

MemoryStream reportMS = new 

MemoryStream();

xslCompiledTransform.Transform(Xmldata, reportArgs, 

reportMS);

For more information

Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
 

 

View Abdel-Rahman Awad's profile on LinkedIn

Archives

 

Rss Feed Tweeter button Facebook button Linkedin button Delicious button Digg button