Posts

Showing posts from 2016

Exception: This document already has a 'DocumentElement' node Exception in C#

Image
While manipulating an XML file I came across with this exception and googled it out for solution. I found many stuffs but none of those were relevant and could not resolve my problem. After few minutes of analysis, I came to know that I was doing something wrong. I was trying to add element directly to XML document, which was not correct. It should be added within the root element. I changed my code accordingly and it was resolved. Full XML file and C# code is given below. Hope this will be helpful for others, and please feel free to add comments or any queries. XML file : C# code : XmlDocument doc = new XmlDocument ();             doc.Load( "xml_file_path" );             // Create new element:             XmlElement book = doc.CreateElement( "Book" );             // Create child elements:             XmlElement title = docDestn.CreateElement( "Title" );             title.InnerText = "Masterin

Microsoft message queue (MSMQ) in C# and .NET

Image
This article is written based on Microsoft Windows 7 using C# and .NET framework 2.0. Microsoft message queue (MSMQ) is one of the technique with which you can achieve communication between two application processes running in same machine, under Windows platform. To use MSMQ, you need to add reference System.Messaging explicitly in to your project and then you can avail its functionalities. Two very basic functions of MessageQueue class are Send() and Receive(). Send() method sends data to the message queue while Receive() method reads data from the message queue. Microsoft Windows OS have three types of message queuing techniques and those are: - 1. Outgoing queue 2. Private queue 3. System queue To view a queue, right click on MyCoputer -> Manage , expand Services and Applications and then expand Message Queuing .  All queue used to enlist here, under the respective category that you have chosen while creating a message queue. C# code t

Pointer in C#, use of unsafe keyword

In this article, we will understand the concept of pointers in C# and how we can use it in our C# code. While learning, I heard that C# does not support pointer which is not exactly true. It does support pointers but we do not have full control over it as in C++. Basically, to maintain type safety and security, C# does not support pointers but by explicit changes we can work with C# pointers. Use of 'unsafe' keyword By using ‘unsafe’ keyword, you can perform an operation that deals with pointers. Code or method used ‘unsafe’ modifier does not mean it is not safe but it is not verifiable by CLR. Unsafe code blocks are not dangerous but its safety does not verified by CLR, means if you are using pointers it is your responsibility to handle security risks and pointer errors. Unsafe keyword can be used with- methods, types or code blocks. Use of ‘unsafe’ keyword introduced security risks, as these are not handled by CLR and it is developer’s responsibility to manage al