Parsing XML in C#: A Quick Working Sample

Just started to work on hacking the Web application of vCAC and REST APIs in C#. As expected, the XML processing is an indispensable part for that purpose. I have worked on C# on and off in the past, but never as consistently as on Java. To get myself familiar with the related C# APIs for parsing, I coded the following sample code.

using System;
using System.Collections.Generic;
using System.Xml;
namespace org.doublecloud
    class XmlParsingDemo
        static void Main(string[] args)
            XmlDocument doc = new XmlDocument();
            XmlNodeList nodes = doc.DocumentElement.SelectNodes("/catalog/book");
            List<Book> books = new List<Book>();
            foreach (XmlNode node in nodes)
                Book book = new Book();
       = node.SelectSingleNode("author").InnerText;
                book.title = node.SelectSingleNode("title").InnerText;
       = node.Attributes["id"].Value;
            System.Console.WriteLine("Total books: " + books.Count);
    class Book
        public string id;
        public string title;
        public string author;

Lost VMs or Containers? Too Many Consoles? Too Slow GUI? Time to learn how to "Google" and manage your VMware and clouds in a fast and secure HTML5 App.

The following is a sample XML for running the code. The content has duplications but is still good for trying out the above program.

<?xml version="1.0"?>
  <book id="bk101">
    <author>Steve Jin</author>
    <title>VMware VI and vSphere SDK</title>
      An in-depth introduction on VMware vSphere SDK and open source vijava API.
  <book id="bk102">
    <author>Steve Jin</author>
      An in-depth introduction on something.
This entry was posted in Software Development and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.


  1. Pierre Lainé
    Posted August 9, 2013 at 4:21 am | Permalink

    Hi steve

    sorry to post here but I didn’t now where to contact you in private.

    I made a tutorial on the VI Java API on the VMware communities web site and maybe it will help developers to use your API.

    You did an amazing job!


  2. Posted August 9, 2013 at 11:57 am | Permalink

    Thanks Pierre,

    Great job in writing up the tutorial! I will add a link to it from the project home.


  3. Posted August 12, 2013 at 1:59 am | Permalink

    Hi Pierre,

    Just updated the project Web site with a news on your tutorial. Also added the link to my existing tutorial.

    Thanks again for your great work!


  4. Pierre
    Posted September 26, 2013 at 1:25 am | Permalink

    Hi sorry for the late reply,
    thanks a lot for adding a link to my tutorial, lot of views since 1 month 😉


  5. Posted September 26, 2013 at 1:52 am | Permalink

    You are very welcome Pierre! Please let me know more good stuff from your side. :)


  6. pierre
    Posted September 26, 2013 at 5:24 am | Permalink

    Yes, still looking for ideas but since I am working for the AREVA group in the VMware team, security didn’t let me try my programs so not a lot of motivation to continue the project…
    Do you mind if I add you on linkedin? my mail address is


  7. Lamia
    Posted October 5, 2013 at 12:33 am | Permalink

    i’m having a problem , could you help me ?

  8. mushy
    Posted October 10, 2013 at 5:54 am | Permalink

    hey i have to ask if i want to print the name and the author of the book what should i do then ?????

  9. CP
    Posted March 3, 2014 at 11:19 am | Permalink


    Good job, quite good for a quick startup :)

  10. Posted April 3, 2014 at 12:55 am | Permalink

    XmlDataDocument xmldoc = new XmlDataDocument();
    XmlNodeList xmlnode ;
    int i = 0;
    string str = null;
    FileStream fs = new FileStream(“product.xml”, FileMode.Open, FileAccess.Read);
    xmlnode = xmldoc.GetElementsByTagName(“Product”);

    Full source :


  11. Praveen
    Posted December 9, 2014 at 1:41 am | Permalink

    Hi, Thank you.
    I was planning to apply the same logic to generate the list of the policies available in my XML file. seems to be not working as i have the tag name like “Q12:Policy”. It is not able to read the tag though i declared it as a string seperatly and called in the list.
    Is there any way i can share my code so that some one can help me out?
    Thanks in advance

  12. Praveen
    Posted December 9, 2014 at 2:02 am | Permalink

    My code is:
    private void button1_Click(object sender, EventArgs e)

    XmlDocument doc = new XmlDocument();

    XmlNodeList nodes = doc.DocumentElement.SelectNodes(“/RSOP/ComputerResults/EntensionData/Extension”);
    String Policy;
    Policy = (“q12:Policy”);
    List books = new List();

    foreach (XmlNode node in nodes)
    Policy Policy = new Policy();

    Policy.Name = node.SelectSingleNode(“q12:Name”).InnerText;
    Policy.Value = node.SelectSingleNode(“q12:Value”).InnerText;
    Policy.catagory = node.Attributes[“q12:Catagory”].InnerText;




    And My XML is :

    Allow active scripting
    Windows Components/Internet Explorer/Internet Control Panel/SecurityPage/Trusted Sites



    And it is having many Policy tags( I just copied only one policy) under .

    Any help would be great.

  13. Ryan
    Posted February 3, 2015 at 6:09 pm | Permalink

    This is awesome… Except that it doesn’t work, at least not in Visual Studio.

    On this line:

    List books = new List();

    “Book” is highlighted as producing an error. The error is:

    The type or namespace ‘Book’ could not be found.

  14. KevvvvyP
    Posted August 4, 2015 at 5:47 am | Permalink

    You’re missing a closing book tag in your xml file.

  15. Posted August 4, 2015 at 3:09 pm | Permalink

    Thanks a lot Kevin! Just updated it.


One Trackback

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


    My company has created products like vSearch ("Super vCenter"), vijavaNG APIs, EAM APIs, ICE tool. We also help clients with virtualization and cloud computing on customized development, training. Should you, or someone you know, need these products and services, please feel free to contact me: steve __AT__

    Me: Steve Jin, VMware vExpert who authored the VMware VI and vSphere SDK by Prentice Hall, and created the de factor open source vSphere Java API while working at VMware engineering. Companies like Cisco, EMC, NetApp, HP, Dell, VMware, are among the users of the API and other tools I developed for their products, internal IT orchestration, and test automation.