Parsing an XML

In this post i am going to explain two different ways to parse XML file. I am directly going to start with the code but before that here is my sample XML. In the code there are two ways of parsing the XML one is the simple way of doing it through which it is done using the XMLReader way and the other is to deserialize it to a class. Both are explained below

 

<catalog>  
    <book id='bk101'>
       <author>Gambardella, Matthew </author>
    </book>
    <book id ='bk102'>
       <author>Ralls, Kim </author>  
    </book> 
</catalog>

The code to parse it is

class Program
	{
		static void Main(string[] args)
		{
			var xmlContents = @"<catalog>  <book id='bk101'><author>Gambardella, Matthew </author></book>
										   <book id ='bk102'><author>Ralls, Kim </author>  </book> 
							</catalog>";
			parseXml(xmlContents);
			serializeXML(xmlContents);
		}
 
		private static void serializeXML(string xmlContents)
		{
			XmlSerializer serializer = new XmlSerializer(typeof(catalog));
			catalog resultingMessage = (catalog)serializer.Deserialize(XmlReader.Create(new StringReader(xmlContents)));
			foreach (var catalogBook in resultingMessage.book)
			{
				Console.WriteLine(catalogBook.author + "    " + catalogBook.id);
			}
		}
 
		private static void parseXml(string xmlContents)
		{
			var output = new StringBuilder();
			using (XmlReader reader = XmlReader.Create(new StringReader(xmlContents)))
			{
				while (reader.ReadToFollowing("book"))
				{
					reader.MoveToFirstAttribute();
					string id = reader.Value;
					output.AppendLine(id);
 
					reader.ReadToFollowing("author");
					output.AppendLine(reader.ReadElementContentAsString());
					output.AppendLine("");
				}
				
			}
 
			Console.WriteLine(output);
		}
	}

In the above code the first method called from main is ParseXML. In this method we are using the simplest way to parse the XML by using the XMLReader. So first we need to create the XMLReader by providing the content of the XML and once we have that then its really easy to go though the XML. The first method used is ‘ReadToFollowing’ this method will take you to the first requested node and each call will traverse to the next occurrence. It returns a bool type based on the result of the search. MoveToFirstAttrubute read the first attribute of the node.

The other way to parse the XML file is to serialize it in object. But you have to have an class object in which you can map the XML file and for complex object its a bit hard to make it. Luckily VS provides a really easy way to do it. All you have to do is select the XML and do the following process

xmltoclass

You will get the following file

[SerializableAttribute()]
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	[XmlTypeAttribute(AnonymousType = true)]
	[XmlRootAttribute(Namespace = "", IsNullable = false)]
	public class catalog
	{
 
		private catalogBook[] bookField;
 
		[XmlElementAttribute("book")]
		public catalogBook[] book
		{
			get
			{
				return bookField;
			}
			set
			{
				bookField = value;
			}
		}
	}
 
 
	[SerializableAttribute()]
	[System.ComponentModel.DesignerCategoryAttribute("code")]
	[XmlTypeAttribute(AnonymousType = true)]
	public class catalogBook
	{
 
		private string authorField;
		private string idField;
		
		public string author
		{
			get
			{
				return authorField;
			}
			set
			{
				authorField = value;
			}
		}
		
		[XmlAttributeAttribute()]
		public string id
		{
			get
			{
				return idField;
			}
			set
			{
				idField = value;
			}
		}
	}

In method serializeXML i am using the deserializer to deserialize the XML to catalog object and then i just loop around the list to display