C# 在C语言中,从XML文件逐节点读取到数组#
XML文件是这样的,大约有20个节点(模块)是这样的C# 在C语言中,从XML文件逐节点读取到数组#,c#,xml,C#,Xml,XML文件是这样的,大约有20个节点(模块)是这样的 <list> <module code="ECSE502"> <code>ECSE502</code> <name>Algorithms and Data structures</name> <semester>1</semester> <prerequisites>none</prerequisites> <lslo
<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>
我执行了以下代码。但当我调试它时,它甚至没有进入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;
}
}
}
请告诉我哪里错了
下面是代码的其余部分
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];
}
}
}
这一行:
XmlNodeList xnList = xml.SelectNodes("list/module[@code='" + text + "']");
应改为:
XmlNodeList xnList = xml.SelectNodes("list/module"); //Does not answer full scope of the question
重读问题后编辑:
OP的代码在我的测试中运行良好。文件路径不正确,或者传递到文本中的字符串s
与读取节点的code
值的大小写匹配
现有的SelectNodes
XPath
区分大小写
您似乎正在使用XPathV1.0,如果这是一个问题,它似乎不支持开箱即用的大小写不敏感。有关执行不区分大小写的XPath搜索的方法,请参见此链接:
另请参见此链接:此行:
XmlNodeList xnList = xml.SelectNodes("list/module[@code='" + text + "']");
应改为:
XmlNodeList xnList = xml.SelectNodes("list/module"); //Does not answer full scope of the question
重读问题后编辑:
OP的代码在我的测试中运行良好。文件路径不正确,或者传递到文本中的字符串s
与读取节点的code
值的大小写匹配
现有的SelectNodes
XPath
区分大小写
您似乎正在使用XPathV1.0,如果这是一个问题,它似乎不支持开箱即用的大小写不敏感。有关执行不区分大小写的XPath搜索的方法,请参见此链接:
另请参见此链接:确保为xml文件指定了正确的路径
它对我有用
确保为xml文件指定了正确的路径
它对我有用
您的代码是正确的,如果输入确实是您显示的,并且s
指向实际存在的代码。由于您是通过相对路径指向文件,请确保您正在加载您真正期望的文件。如果输入是您实际显示的输入,则代码是正确的,,并且s
指向实际存在的代码。由于您是按相对路径指向文件,请确保您正在加载您真正期望的文件。发现错误。我向writeToXML方法传递了一个错误的值。我没有传递代码,而是传递了name找到了错误。我向writeToXML方法传递了一个错误的值。我没有传递代码,而是传递了name这将适用于这个单独的示例,但我认为很明显,他希望能够通过code
属性指定模块(因为这是字符串参数的唯一用法)…如何传递他想要读取节点的code
值这将适用于这个单独的示例,但我认为很明显,他希望能够通过code
属性指定模块(因为这是字符串参数的唯一用法)…您如何传递他想要读取节点的code
值您的代码对我有效。您是否将正确的代码传递给writeToXML
?您的代码正在工作。请测试text
值是否有效code
调试应用程序,在xml之后放置断点。加载并确认text
和xml
的值。文本的值是字符串。XML节点模块code=
大小写是否与列表框1中的大小写匹配。在转换为字符串后,选择editem
,您的代码对我有效。您是否将正确的代码传递给writeToXML
?您的代码正在工作。请测试text
值是否有效code
调试应用程序,在xml之后放置断点。加载并确认text
和xml
的值。文本的值是字符串。XML节点module code=
大小写是否与listbox1中的大小写匹配。在转换为string
I did,XML.Load(@“D:\Modules.XML”)后,选择editem
;我已经在我的机器上测试过了,它正在工作。试着调试你的代码。它们一定是其他问题。我确实是这样做的,xml.Load(@“D:\Modules.xml”);我已经在我的机器上测试过了,它正在工作。试着调试你的代码。他们肯定是其他问题。