2013-03-29 48 views
0

這裏是我的XML代碼(module.xml)如何通過它的atrribute名稱選擇一個節點並在C#中獲取它的子節點?

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

回答

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 
} 
+0

沒有工作。和它在做什麼, XmlNodeList xnList = xml.SelectNodes(「/ module [@code ='」+ text +「'」「); – Ravindu

+1

嘗試將您的模塊封裝在根標記中。如果

+0

,它只會找到那些代碼屬性爲 –

2

你可以使用LINQ to XML,assum E在這裏的代碼返回匿名對象的名單,但如果你願意,你可以明確地定義你自己的類:

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"), 
           //..... 
          }); 
+0

我試過@Manish Mishra的方式。它正在工作。 – Ravindu

+1

@Ravindu:嘗試這種方式:) –

+0

好吧,我也會試試這個 – Ravindu

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> 
相關問題