简体   繁体   English

在C#中从XML文件逐节点读取到Array节点

[英]Read from a XML file to an Array node by node in C#

The XML file is like this,There are about 20 Nodes(modules) like this. XML文件是这样的,大约有20个节点(模块)。

<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. 但是,当我调试它时,它甚至没有进入foreach函数。

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

        }
    }
}

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. OP的代码在我的测试中工作正常。 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. 文件路径不正确,或者传递到text中的string s与您读取节点所依据的Code值的大小写匹配。

The SelectNodes XPath as you have it is case sensitive. 您拥有的SelectNodes XPath区分大小写。

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. 您似乎正在使用XPath V1.0,如果出现问题,它似乎不支持开箱即用的不区分大小写。 See this link for a way to perform case insensitive XPath searches: http://blogs.msdn.com/b/shjin/archive/2005/07/22/442025.aspx 有关执行不区分大小写的XPath搜索的方法,请参见以下链接: http : //blogs.msdn.com/b/shjin/archive/2005/07/22/442025.aspx

See also this link: case-insensitive matching in xpath? 另请参见以下链接: xpath中是否区分大小写?

Make sure you are specifying correct path for your xml file. 确保为xml文件指定正确的路径。

It is working for me. 它为我工作。

在此处输入图片说明

Your code is correct, if the input is really the one you shown , and s point to an actual present code . 您的代码是正确的, 如果输入的是真的,你出的一个 ,和s 指向一个实际存在的代码 Since you are pointing the file by a relative path, ensure you are loading the file you really expect. 由于您是通过相对路径指向文件,因此请确保正在加载您真正期望的文件。

Found the error. 发现错误。 I was passing a wrong value to writeToXML method. 我将错误的值传递给writeToXML方法。 Insted of passing code, I have passed name 插入了代码,我已经通过了名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM