Friday, October 10, 2008

Sample to Read & Write XML File through C#

Hi,

Here is the sample to read and write xml file through C# code. In order to explain the process in a simple fashion, I made use of a xml file called SomeName.xml (The contents of the xml file are shown below in Section #1). Through C# code i am reading the nodes and attribute values which any one would prefer to do in their application. Then to explain the process of writing a xml file into file system, I am making use of the values I read from input xml file. I create a XmlDocument Object in memory, create nodes and with the read values append them inside and write them to a new XML file in file system called WrittenFile.xml.


Section #1 has the content of input xml file, Section #2 contains the c# code to be placed in pageload event & section #3 has the content of generated output xml file.

Section #1:

Input XML file: ~\XML\SomeFolder\SomeName.xml

<?xml version="1.0" standalone="yes" ?>

<report>

<table>

<columns>

<column name="Attribute1" />

<column name="Attribute2" />

</columns>

<rows>

<row Attribute1="6693" Attribute2="655" />

<row Attribute1="432" Attribute2="364" />

<row Attribute1="34" Attribute2="766" />

<row Attribute1="665493" Attribute2="134" />

<row Attribute1="56783" Attribute2="656" />

</rows>

</table>

</report>

Section #2:

C# Code written in page load event:

protected void Page_Load(object sender, EventArgs e)

{

try

{

//Creating XmlDocument Class object:

XmlDocument oXmlDocument = new XmlDocument();

//Loading the XML file from File System:

oXmlDocument.Load(Server.MapPath("XML/SomeFolder/SomeName.xml"));

//1.To Read a Single Node

//Giving the Xpath of the Node: eg: //report/table/rows/row

//Assigning the returned XmlNoded from SelectSingleNode Method:

XmlNode oXmlElementCampaignName = oXmlDocument.DocumentElement.SelectSingleNode("//report/table/rows/row");

//2.To Read Collection of Nodes

//Giving the Xpath to method SelectNodes eg: SelectNodes("//report/table/rows/row");

//Assigning the returned XmlNoded from SelectSingleNode Method:

XmlNodeList oXmlNodeList = oXmlDocument.DocumentElement.SelectNodes("//report/table/rows/row");

// Create a XmlDocument object to write a file:

XmlDocument xmlDocument = new XmlDocument();

// Create a XmlElement which represents an Element : Root Element

XmlElement Items = xmlDocument.CreateElement("Items");

//Iterate through every nodes in oXmlNodeList which is a collection of Nodes

foreach (XmlNode objNode in oXmlNodeList)

{

// Create a XmlElement which represents an Element : Node

XmlElement Item = xmlDocument.CreateElement("Item");

// Create Child Node XmlElement1

XmlElement xmlelement1 = xmlDocument.CreateElement("XmlElement1");

// Sets value for Child Node

XmlText xmlelement1_text = xmlDocument.CreateTextNode(objNode.Attributes["Attribute1"].

Value.ToString());

// Placed inside Child Node

xmlelement1.AppendChild(xmlelement1_text);

XmlElement xmlelement2 = xmlDocument.CreateElement("XmlElement2");

XmlText xmlelement2_text = xmlDocument.CreateTextNode(objNode.Attributes["Attribute2"].

Value.ToString());

xmlelement2.AppendChild(xmlelement2_text);

// Placing under Node

Item.AppendChild(xmlelement1);

// Placing under Node

Item.AppendChild(xmlelement2);

// Placing under Root

Items.AppendChild(Item);

}

// Placing under XML document

xmlDocument.AppendChild(Items);

// Writing the XML file into file system

xmlDocument.Save(Server.MapPath("XML/SomeFolder/WrittenFile.xml"));

}

catch (Exception ex)

{

//Exception block here

}

}

Section #3:

Output XML file: ~\XML\SomeFolder\WrittenFile.xml

<Items>

<Item>

<XmlElement1>6693</XmlElement1>

<XmlElement2>655</XmlElement2>

</Item>

<Item>

<XmlElement1>432</XmlElement1>

<XmlElement2>364</XmlElement2>

</Item>

<Item>

<XmlElement1>34</XmlElement1>

<XmlElement2>766</XmlElement2>

</Item>

<Item>

<XmlElement1>665493</XmlElement1>

<XmlElement2>134</XmlElement2>

</Item>

<Item>

<XmlElement1>56783</XmlElement1>

<XmlElement2>656</XmlElement2>

</Item>

</Items>

Hope this should be helpful for people who are looking to samples to read or write xml file.

Regards
Mohammed Fauzi

Wednesday, October 8, 2008

Automate MOSS 2007 Backup

Hi...

To automate the backup activity of MOSS 2007 portal Server we use to do the following two steps...
1. Creation of Batch file: Here we create a batch file which will execute the stsadm.exe, passing necessary parameters to perform the backup activity in command prompt.

Batch file Code as follows:

@echo off
echo ====================================================
echo Backup Script For Office SharePoint Server 2007
echo ====================================================
@echo off

RD D:\Backup\SOME FOLDER /S /Q
MD D:\Backup\SOME FOLDER

stsadm.exe -o backup -directory "\\SOME FOLDER" -backupmethod full -item "Farm" -overwrite

echo completed

2. Scheduled Tasks: In this step we can create a scheduled task (Control Panel --> Scheduled Tasks) to execute the batch file at a preferred frequency of time.

Thus your backup is automated, its advisable to the monitor free space available on the disk regularly...

Now recently there are new tools available to take backups.And they come with more cool features. If you use some interesting tools or any other technique, feel free to let us know :)

Warm Regards
Mohammed Fauzi ~ 4Z

Tuesday, October 7, 2008

Common Method to Log Exception - C#

Hi...

As per friend's request i am posting the code for a common method to log the exceptions which may occur in you application;
We can place this code in one of your common classes and call this method from the Catch block passing the exception object as its parameter.

Following is the code;

public void logException(Exception ex)

{

string strMethodName = ex.TargetSite.Name;

string strClassName = ((System.Reflection.MemberInfo)(ex.TargetSite)).ReflectedType.Name;

string strErrLineNo = ex.StackTrace;

StreamWriter srWriterObj = new StreamWriter(HttpContext.Current.Request.PhysicalApplicationPath +@"\Zlog\Exception.log", true);

srWriterObj.NewLine = ("\r\n");

srWriterObj.WriteLine ("Error: " + ex.Message + " on " + System.DateTime.Now.ToString());

srWriterObj.NewLine = ("\r\n");

srWriterObj.WriteLine("MethodName: " + strMethodName + " | " + "ClassName: " + strClassName + " | " + "ErrLineNo: " + strErrLineNo);

srWriterObj.Flush();

srWriterObj.Close();

}


Just create the object of the class and call your exception method as shown below to log exceptions;

catch (Exception ex)

{

GServices objGServices = new GServices();

objGServices.logException(ex);

}

Note: Additional Namespace to be added for the above code are System.Reflection and System.IO

Regards
Mohammed Fauzi ~ 4Z

Saturday, September 13, 2008

CERN Large Hadron Collider (LHC)

Hi...

Found this interesting video on youtube yesterday.
It explains clearly what happens in the LHC, the BIG Event to understand the concepts of Big-Bang theory was successfully executed by CERN team last Wednesday (10 September 2008) in Geneva, Switzerland.

In my view it is not a waste of time or Money. It's an effort which should be Appreciated. More over I came to know there are myths about this event that it might become a disaster to the world and some people fear it might create a black hole on earth and so on... It's a well controlled observation environment and all safety factors should have been taken care. So no need to worry, relax... and Enjoy the video :)

Video:



Pictures:
Click Here

Regards
Mohammed Fauzi

Tuesday, September 9, 2008

To display Percentage symbol in Fusion chart

Hi,

In fusioncharts if you give numbersuffix='%' alone, it will not work. You need to give numbersuffix='%25' to display '%' as suffix.

Sunday, September 7, 2008

Solution for SSIS job : The package execution failed. The step failed.

Hi...

I created a dtsx package which will fetch data from Excel file and insert them into respective SQL Server table. When i run the package from SQL Server Business Intelligence Development studio it works fine. When i run package from Command prompt also it works fine. But when we create it as a SQL Server Agent Job, It says the following error.

The package execution failed. The step failed.

I found this article from Microsoft Support team for all scenarios while execution of Job failed.
http://support.microsoft.com/kb/918760

It was clear, in my case it was permission issue, So i kept looking for blogs on such case. Managed to find this blog which helped me to solve the issue.

Following are two points which i believe, did the trick for me...

1. Under SQL Server Configuration Manager:

* Right click SQL Server Agent(MSSQLSERVER)

SQL Server Configuration Manager

* Click Properties

* Select radio button this Account, Gave the User Name & Password (the user name with which i have created the .dtsx package )

Click Properties

2. In Computer Management(Right click on my computer & Click Manage)

* Under Local Users and Groups
* Under Groups
* Right Click SQLSERVER2005SQLAGENTUSER$YourServerName$MSSQLSERVER
* In Properties add the user name with which i have created the .dtsx package

In Computer Management

These steps solved my problem, Hope this should be helpful :)

Regards
Mohammed Fauzi

Wednesday, September 3, 2008

Google Chrome : Solution : Application failed to initialize properly (0xc0000005)

Hi...

Here is the solution for the error : "Application failed to initialize properly (0xc0000005)" when you run Google Chrome after installation.

This is for Symantec EP users, you need to change the following registry entry and restart the machine.

In Regedit:

HKEY_LOCAL_MACHINE -> SYSTEM -> CurrentControlSet -> Services -> SysPlant

Edit Value of Start REG_DWORD to 4

Got this solution from Symantec Forum. Now Chrome works fine on my machine!

Enjoy Chromeing :)