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

时间:2013-03-29 07:06:48

标签: c# xml visual-studio-2010

这是我的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);

3 个答案:

答案 0 :(得分: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"),
                                 //.....
                             });

答案 1 :(得分: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
}

答案 2 :(得分: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>