Click here to Skip to main content
15,891,896 members
Articles / DevOps
Tip/Trick

How to Select XML Node by Name in C#

Rate me:
Please Sign up or sign in to vote.
3.06/5 (8 votes)
3 Aug 2016CPOL 19.9K   132   15   2
How to Select XML Node by name in C#

Introduction

Let’s first establish what the purpose of code is in the first place.

For this, the purpose of code is to "How To Select XML Node by name in C#". We use MVC(C#) for creating this demo.

We use XPath Expression for selecting the XML node.

What is XPath?

XPath is a path expressions to select nodes or node-sets in an XML document.

Using the Code

Now, we have the following XML document. Save it as demo.XML.

XML
<info>
  <collage>
    <name>SIGMA INSTITUTE</name>
    <students>650</students>
  </collage>
  <collage>
    <name>ORCHID INSTITUTE</name>
    <students>1200</students>
  </collage>
</info>

We want to get all <collage> nodes. So, our XPath Expression is "/info/collage".

Create the Below ActionResult Method

C#
public ActionResult Index()
{
    try
    {
        //Create A XML Document Of Response String
        XmlDocument xmlDocument = new XmlDocument();

        //Read the XML File
        xmlDocument.Load("D:\\demo.xml");

        //Create a XML Node List with XPath Expression
        XmlNodeList xmlNodeList = xmlDocument.SelectNodes("/info/collage");

        List<Info> infos = new List<Info>();

        foreach (XmlNode xmlNode in xmlNodeList)
        {
            Info info = new Info();
            info.CollageName = xmlNode["name"].InnerText;
            info.Students = xmlNode["students"].InnerText;

            infos.Add(info);
        }

        return View(infos);
    }
    catch
    {
        throw;
    }
}

Create the Below Class & Declare Properties

C#
public class Info
{
    public string CollageName { get; set; }

    public string Students { get; set; }
}

Create a View

HTML
@model IEnumerable<SelectXMLNode.Controllers.HomeController.Info>

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
</head>
<body>
    <div>
        @if (Model.Count() > 0)
        {
            foreach (var item in Model)
            {
                <div class="row">
                    <div class="col-md-6">Collage: @item.CollageName </div>
                    <div class="col-md-6">Number Of Students: @item.Students</div>
                </div>
            }
        }
    </div>
</body>
</html>

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Comments and Discussions

 
QuestionLucid piece of work Pin
MarcusCole68333-Aug-16 10:59
professionalMarcusCole68333-Aug-16 10:59 
AnswerRe: Lucid piece of work Pin
Khunt Suchit3-Aug-16 19:23
professionalKhunt Suchit3-Aug-16 19:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.