Convert an Object to XML in C#


private string GetObjectAsXml(object obj)

{

PropertyInfo[] properties =  obj.GetType().GetProperties();

StringBuilder builder = new StringBuilder();

builder.AppendFormat(“<{0}>”, obj.GetType().Name);

foreach (PropertyInfo property in properties)

{

object propertyValue = property.GetValue(obj, null);

builder.AppendFormat(“<{0}>{1}</{0}>”, property.Name, propertyValue);

}

builder.AppendFormat(“</{0}>”, obj.GetType().Name);

return builder.ToString();

}

Validate File Upload Control to upload certain extensions


we will take an example to upload images only with this extensions ” jpg , jpeg , png , gif ”

<script type=“text/javascript”>

function checkImageExtension(elem) {

var filePath = elem.value;

if (filePath.indexOf(‘.’) == -1)

return false;

var validExtensions = new Array();

var ext = filePath.substring(filePath.lastIndexOf(‘.’) + 1).toLowerCase();
validExtensions[0] = ‘jpg’;

validExtensions[1] = ‘jpeg’;

validExtensions[2] = ‘png’;

validExtensions[3] = ‘gif’;
for (var i = 0; i < validExtensions.length; i++)

{

if (ext == validExtensions[i])

return true;

else

elem.value = ‘ ‘;

}

alert(ext.toString() + ‘\n’ + ‘Disabled’);

return false;

}

<asp:FileUpload ID=”fuAttachment” runat=”server” />

C#:

protected void Page_Load(object sender, EventArgs e)

{            fuSelectFile.Attributes.Add(“onchange”, “return checkImageExtension(this);”);        }

Screen shot