I need to write an event system in Tridion so that if any page modified/Created in publication and publish the same then the sitemap file placed in that publication get published automatically at the time when web master/content author do this manually. What is the correct way to do this?
The thing to watch out for here is that your Sitemap could end up getting published very frequently, and occupying all your publishing threads.
Perhaps it's not the case you want it submitting to the publishing queue every time a page is published?
This could cause it to be in the queue multiple times.
This Stack Overflow post explains the problem well, and provides some possible solutions.
Bart Koopmans answer will prevent the SiteMap from entering the publishing queue more than once at least.
Per your clarification in the comment, you need to update your sitemap.xml only on the Post Publishing event (and not when you create/modify) as your topnav, leftnav and breadcrumb will be created/modified once your page is published - just ask yourself, will it be the case that you create a new page, do not publish it and updating the sitemap - To me it does not make sense.
Based on this, I can suggest following options (in order of their recommendation):
Write a Custom resolver, put up appropriate logic into it. You may search the net for Custom Resolver Pros: High Performance, Most Control on the entire flow Cons: High implementation effort
Write a storage extension for Page and write logic to checks whether the page published is a new page and is published first time or if it is an older page being published, then what are the changes from its last published version. If these are the changes in path, name, url etc. - Publish the SiteMap.xml as well programatically. Pro: Provide most control on the whole process and high performance. Cons: High Implementation effort
Write a C# TBB which checks whether the page published is a new page and is published first time or if it is an older page being published, then what are the changes from its last version. If these are the changes in path, name, url etc. - Publish the SiteMap.xml as well programatically - there might be few scenario that you may not have control in this process based on your business requirement. Pro: Easy to implement, High Performance Cons: Not highly scalable and might not meet future needs
Event System: Write a custom event handler for Post Publishing phase and apply the logic mentioned in the Step - 2 Pros: Easy to implement Cons: Performance hit, Scalability.
Write a C# TBB, attach it with every page template Or Write a Custom Event Handler on Publish Post event and publish the sitemap.xml from it Pros: least implementation effort Cons: High performance hit and high resource utilization
I wouldn't go for the event system in this case (if sitemap is big and the event system has to loop through all pages on each publish action and perform extra checks on publishing states etc. you could end up having performance issues.
You could check my answer in post What are the best practices to manage the sitemap for a Tridion website?, and a blog post on http://tridionrevealed.wordpress.com/2012/01/25/custom-deployer-sitemap/ to find some code samples to make a custom deployer/sitemap helper to move the logic to the front-end.
Event System is good option for this. On Page Save and close event you can do your processing of sitemap creation and publish the sitemap page.
For Event System Help read Blog