Generic List of Objects to an XML = Serialization

Here is the code, it’s straight forward.

private string SerializeGenericList(List<Object> _list)
 {
 string _result = String.Empty;

 try
 {
 XmlSerializer ser = new XmlSerializer(_list.GetType());
 StringBuilder sb = new StringBuilder();
    using (StringWriter writer = new StringWriter(sb))
    {
       ser.Serialize(writer, _list);
      _result = sb.ToString();
      }
 }
 catch (Exception ex)
 {
 }
 return _result;
 }
The function returns a string so we can easly create an XML.
Next thing you could is :

XmlDocument doc = new XmlDocument();
doc.LoadXml(SerializeGenericList(_list)); //_list is your Genereci List<object>
 
Hope it helps !!!



Published: 12/22/2011 | 1  Comment | 0  Links to this post

SharePoint postback problem after file download

For a customer I was creating a project where they needed to click a button and download a file.
This is basic ASP.net stuff, BUT in SharePoint there are some issues with this.

If you use this code, which is the code to push a file towards the client browser for download:

Response.Clear();
Response.Buffer = true;
Response.BufferOutput = true;
Response.ContentType = "application/x-download";
Response.AddHeader("Content-Disposition", "attachment; filename=test.xml");
Response.CacheControl = "public";
Response.OutputStream.Write(buffer, 0, buffer.Length);
Response.Flush();

This code will work once,BUT next time you click the button it will not work.
Note: I’m using a byte array to publish, reason is that I created in this case an XML at runtime, this will be handled in an other blog

Solution:

We need to add Javascript to the button !!! Glimlach

 protected override void OnLoad(EventArgs e)
{
           public string ScriptRefresh =@"function Refresh()
                                        {
                                           window.setTimeout(
                                               function()
                                               {
                                                  _spFormOnSubmitCalled = false;
                                                }, 10);
                                           } ";  //This string represents the javascript fore this issue, it could be any kind of javascript.

this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"Refresh",ScriptRefresh, true);     //Here we add the Javascript to the page ,ScriptRefresh = javascript, you could type it directly here if you want
btnDownLoadXML.OnClientClick =
"Refresh()";  //Here we add the javascript to the button

}

Thats actually all you need to do.
What you are doing behind the scene is tell SharePoint that the page is not submitted yet.

Hope it helps


Published: 12/22/2011 | 0  Comments | 0  Links to this post

Beste way to remove HTML tags…

At a sudden moment you will come agross this issue.

You have an HTML stirng, ans all those tags should me removed.

There are severals ways to handle this issue,but this one solved my issue’s untill now:

 

return Regex.Replace(text, @”<(.|\n)*?>”, string.Empty);

 

Hope it helps you


Published: 12/5/2011 | 0  Comments | 0  Links to this post