如何在c#中通过节点的属性名选择节点并获取它的子节点

本文关键字:节点 选择 获取 子节点 属性 | 更新日期: 2023-09-27 18:06:21

这是我的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个节点。

I try this so far

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

如何在c#中通过节点的属性名选择节点并获取它的子节点

您可以使用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"),
                                 //.....
                             });

try this:

将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
}

用下面的代码在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>