0

The XML file is like this,There are about 20 Nodes(modules) like this.

<list>
<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 with totorials and inclass tests</description>
</module>    
</list>

I did the following code. But when I debugged it it even didn't went inside to foreach function.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace ModuleEnrolmentCW
{
    class XMLRead
    {

        public string[] writeToXML(string s)
        {
            string text = s;           
            string[] arr = new string[6];

            XmlDocument xml = new XmlDocument();
            xml.Load("modules.xml");

            XmlNodeList xnList = xml.SelectNodes("list/module[@code='" + text + "']");
            foreach (XmlNode xn in xnList)
            {
                arr[0] = xn.SelectSingleNode("code").InnerText;
                arr[1] = xn.SelectSingleNode("name").InnerText;
                arr[2] = xn.SelectSingleNode("semester").InnerText;
                arr[3] = xn.SelectSingleNode("prerequisites").InnerText;
                arr[4] = xn.SelectSingleNode("lslot").InnerText;
                arr[5] = xn.SelectSingleNode("tslot").InnerText;                            
            }

            return arr;
        }


    }
}

Please tell me where is the wrong??

Here is the rest of the code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ModuleEnrolmentCW
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string selected;
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            XMLRead x = new XMLRead();
            selected = (string)listBox1.SelectedItem;
            string[] arr2 = x.writeToXML(selected);

            label11.Text = arr2[0];

        }
    }
}
Ravindu
  • 2,408
  • 8
  • 30
  • 46

4 Answers4

1

This line:

XmlNodeList xnList = xml.SelectNodes("list/module[@code='" + text + "']");

should read:

XmlNodeList xnList = xml.SelectNodes("list/module"); //Does not answer full scope of the question

Edit following a reread of the question:

The OP's code works fine in my tests. Either the file path is not correct, or the the string s passed into text matches the case of the Code value by which you are reading the nodes.

The SelectNodes XPath as you have it is case sensitive.

You appear to be working with XPath V1.0 which doesn't appear to support out of the box case insensitivity if that's a issue. See this link for a way to perform case insensitive XPath searches: http://blogs.msdn.com/b/shjin/archive/2005/07/22/442025.aspx

See also this link: case-insensitive matching in xpath?

Community
  • 1
  • 1
jordanhill123
  • 4,142
  • 2
  • 31
  • 40
  • 1
    This will work for this isolated example but I think it's pretty clear he wants to be able to specify modules by their `code` attribute (given that that's the only use of the string parameter)... – Ant P Mar 29 '13 at 09:56
  • How can you pass the `Code` value for which he wants to read the nodes – Rohit Vyas Mar 29 '13 at 10:00
1

Make sure you are specifying correct path for your xml file.

It is working for me.

enter image description here

Kapil Khandelwal
  • 15,958
  • 2
  • 45
  • 52
1

Your code is correct, if the input is really the one you shown, and s point to an actual present code. Since you are pointing the file by a relative path, ensure you are loading the file you really expect.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
0

Found the error. I was passing a wrong value to writeToXML method. Insted of passing code, I have passed name

Ravindu
  • 2,408
  • 8
  • 30
  • 46