0

这是我的 XML 代码(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>

根据上面的 xml 代码,我需要通过只给 ECSE502 作为输入来获取集合。选择所需节点后,我还需要获取其子节点的值(名称、学期等)。在 XML 文件中有 20 个节点。这只是第一个 2 个节点。

到目前为止我试过这个

 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);
4

3 回答 3

2

您可以使用LINQ to XML,假设这里的代码返回匿名对象列表,但如果需要,您可以显式定义自己的类:

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"),
                                 //.....
                             });
于 2013-03-29T07:15:35.860 回答
1

试试这个:

将您的 xml 包装在一个唯一的根节点中,即

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

进而

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
}
于 2013-03-29T07:11:14.473 回答
1

使用以下代码在 xml 中创建根节点后尝试

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();

你的 XML 应该是这样的:

<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>
于 2013-03-29T07:29:35.747 回答