Ravindu
Ravindu

Reputation: 2550

How to select a Node by it's atrribute name and get it's child node in C#?

Here is my XML code(module.xml)

<module code="ECSE502">
  <code>ECSE502</code>
  <name>Algorithms and Data structures</name>
  <semester>1</semester>
  <prerequisites>none</prerequisites>
  <lslot>0</lslot>
  <tslot>1</tslot>
  <description>all about algorythms and data structers</description>
</module>
<module code="EIGA501">
  <code>EIGA501</code>
  <name>3D Grapgics I</name>
  <semester>1</semester>
  <prerequisites>none</prerequisites>
  <lslot>2</lslot>
  <tslot>3</tslot>
  <description>xxxxxxxxxxxxxxxxxxxxxx</description>
</module>

According to the above xml code, I need to get the set by only giving ECSE502 as the input. After selecting the required node I need to get its child node's values also(name, semester,etc.). In the XML file there are 20 nodes. this is only the 1st 2 nodes.

I tried this so far

 XmlTextReader reader = new XmlTextReader("modules.xml");
            XmlDocument doc = new XmlDocument();
            XmlNode node = doc.ReadNode(reader);

        foreach (XmlNode chldNode in node.ChildNodes)
            Console.WriteLine(reader.Value);

Upvotes: 0

Views: 99

Answers (3)

Rohit Vyas
Rohit Vyas

Reputation: 1969

Try it after creating a root node in your xml with below code

XDocument xdoc = XDocument.Load(@"D:\data\rvyas\Projects\Client\module.xml");


string code = "ECSE502";
    var result = xdoc.Descendants("module")
                .Where(x => (string)x.Element("code") == code)
                .Select(x => new
                {
                    Name = (string)x.Element("name"),
                    Code = (string)x.Element("code"),
                    semester = (string)x.Element("semester"),
                    prerequisites = (string)x.Element("prerequisites"),
                    lslot = (string)x.Element("lslot"),
                    tslot = (string)x.Element("tslot")

                }).ToList();

You XML should be like:

<root>
<module code="ECSE502">
  <code>ECSE502</code>
  <name>Algorithms and Data structures</name>
  <semester>1</semester>
  <prerequisites>none</prerequisites>
  <lslot>0</lslot>
  <tslot>1</tslot>
  <description>all about algorythms and data structers</description>
</module>
<module code="EIGA501">
  <code>EIGA501</code>
  <name>3D Grapgics I</name>
  <semester>1</semester>
  <prerequisites>none</prerequisites>
  <lslot>2</lslot>
  <tslot>3</tslot>
  <description>xxxxxxxxxxxxxxxxxxxxxx</description>
</module>
</root>

Upvotes: 1

Manish Mishra
Manish Mishra

Reputation: 12375

try this:

wrap your xml in a unique root node i.e

<modules>
   <module code...
       ..
   </module>
   <module code...
</modules>

and then

string text="ECSE502";

XmlDocument xml = new XmlDocument();
xml.Load("physical path to module.xml"); 

XmlNodeList xnList = xml.SelectNodes("modules/module[@code='"+text+"']");
foreach (XmlNode xn in xnList)
{
    string code = xn.SelectSingleNode("code").innerText;
    string name=  xn.SelectSingleNode("name").innerText;
    //and similarly find other inner nodes
}

Upvotes: 1

cuongle
cuongle

Reputation: 75316

You can use LINQ to XML, assume in here the code returns the list of anonymous objects, but you can explicitly define your own class if you want:

var xDoc = XDocument.Load("yourpathfile");
var result = xDoc.Descendants("module")
            .Where(x => (string) x.Element("code") == "ECSE502")
            .Select(x => new
                             {
                                 Name = (string)x.Element("name"),
                                 //.....
                             });

Upvotes: 2

Related Questions