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];

        }
    }
}
5
  • 1
    Your code works for me. Do you pass correct code to writeToXML? Commented Mar 29, 2013 at 9:57
  • Your code is working have you test the text value to be a valid code Commented Mar 29, 2013 at 9:58
  • Debug your application, put a breakpoint after xml.Load and confirm the values of text and xml. Commented Mar 29, 2013 at 10:09
  • text's value is a string. It passes correct Commented Mar 29, 2013 at 10:15
  • 1
    Does the XML node module code= case match with the case in listbox1.SelectedItem after casting to string Commented Mar 29, 2013 at 10:17

4 Answers 4

1

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

It is working for me.

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I did , xml.Load(@"D:\Modules.xml"); and put the file in my D partition.NOT working
I have tested it on my machine it is working. Try to debug your code. Their must be some other issue.
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?

2 Comments

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)...
How can you pass the Code value for which he wants to read the nodes
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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.