Friday, April 22, 2011

Handling special characters in XML strings in C#.Net

There are set of characters that cannot be used in normal in XML a strings. If there are any of these characters in a xml string it will break xml parsing. But if you still want to include these characters in xml string we have to replace those characters according to the following list. 

















If you need to construct xml strings in a C# program you can escape those characters using
SecurityElement.Escape(XmlValue) method given by the .Net framework. 











Using above method you can easily pass those special charactors via xml string without any trouble. 


Tuesday, April 5, 2011

LINQ to XML - Samples

If you are developing a relatively small application, most probably you might opt to have an in memory database rather than having a SQL or Access or any other heavy weight database. So that you can develop and deploy your application without need of a database server. For this we can go for several options like plain text files, xml files etc.

Because of lots of benefits most of the times you will go for a xml database. In previous versions of .Net it was bit difficult to access and manipulate the those xml databases. But with the introduction of LINQ (with .Net framework 3.5) accessing and manipulation became very easier than it was before. In this article I wont go for much details about LINQ and its benefits and other details in very deep since there are lots of documentation in the web. So here I will just give some samples for some of the main data access and manipulation operations.

Consider the following XML file, 


01) Select operation

        private void GetUsers()
        {
            label1.Text = string.Empty;
            XDocument xmlDB = XDocument.Load("TestDB.xml");

            var users = from user in xmlDB.Descendants("User")
                        select new
                        {
                            UserID = user.Attribute("id").Value,
                            FirstName = user.Element("FirstName").Value,
                            LastName = user.Element("LastName").Value,
                        };

            foreach (var user in users)
            {   //Here im just writing the out put in to a labal. You can do what ever you want as appropriately.
                label1.Text += user.UserID + "\n" + user.FirstName + "\n" + user.LastName + "\n\n";
            }
        }       

02) Insert New User

        private void AddUser()
        {
            XDocument xmlDoc = XDocument.Load("TestDB.xml");          
            int userID = 0;
            userID = xmlDoc.Descendants("User").Max(id => (int)id.Attribute("id"));
            userID += 1;        

            xmlDoc.Root.Element("Users").Add(
                new XElement("User",
                       new XElement("FirstName", "Wijaya"),
                       new XElement("LastName", "Wijenayake"),
                       new XAttribute("id",userID.ToString())
                ));
            xmlDoc.Save("TestDB.xml");
        }

03) Update existing user

        private void UpdateUser(int userID)
        {
            XDocument xmlDoc = XDocument.Load("TestDB.xml");
            XElement user = xmlDoc.Root.Element("Users").Elements("User").Where(u => (int)u.Attribute("id")                                                                                                          == userID).FirstOrDefault();

            if (user != null)
            {
                user.SetElementValue("FirstName", "Viraj");
                xmlDoc.Save("TestDB.xml");
            }          
        }

04) Delete existing user

        private void DeleteUser(int userID)
        {
            XDocument xmlDoc = XDocument.Load("TestDB.xml");
            XElement user = xmlDoc.Root.Element("Users").Elements("User").Where(u =>                                (int)u.Attribute("id") == userID).FirstOrDefault();

            if (user != null)
            {
                user.Remove();
                xmlDoc.Save("TestDB.xml");
            }          
        }