2010-03-10 25 views
0

的計數我有一點名爲sample.xml中的XML文件的哪個如下所示如何確定標籤

<?xml version="1.0" encoding="ISO-8859-1"?> 

<countries> 

<country> 
    <text>Norway</text> 
    <value>N</value> 
</country> 

<country> 
    <text>Sweden</text> 
    <value>S</value> 
</country> 

<country> 
    <text>France</text> 
    <value>F</value> 
</country> 

<country> 
    <text>Italy</text> 
    <value>I</value> 
</country> 

</countries> 

我已經按鈕命名提交(按鈕1)。如果我點擊該按鈕,我需要顯示在一個名爲textBox1的文本框算(分區名=「AIX」),是指有多少分區名=「AIX」是屬於TYPE =「NIC」

任何一個可以給我的C#代碼

我不喜歡這,,但不能夠得到answaer

private void button1_Click(object sender, EventArgs e) 
    { 
     XmlDocument doc1 = new XmlDocument(); 
     doc1.Load(@"D:\New Folder\WindowsFormsApplication3\WindowsFormsApplication3\Sample.xml"); 
     XmlNodeList a = doc1.GetElementsByTagName("AIX"); 
     textBox1.Text = a.Count.ToString(); 
    } 
+0

發佈您到目前爲止已完成的代碼,並且人們可以幫助您解決問題。沒有人會爲你寫代碼。 – 2010-03-10 15:54:35

+0

我貼了代碼,我做了 – peter 2010-03-10 16:27:16

回答

0

XmlDocument doc1 = new XmlDocument(); doc1.Load(@「C:\ Labs \ test.xml」);

 XmlNodeList nodes = doc1.GetElementsByTagName("inv:Port"); 
     int count = 0; 
     foreach (XmlNode childNode in nodes) 
     { 
      XmlNodeReader nodeReader = new XmlNodeReader(childNode); 

      while (nodeReader.Read()) 
      { 
       if (nodeReader.GetAttribute("PartitionName") == "AIX") 
       { 
        count++; 
       }    

      }    
     }  

     Console.WriteLine("Count = {0}", count); 
     Console.ReadLine();  
+0

這是工作正常,,讓我試着優化代碼,最後一步 – peter 2010-03-10 17:06:32

+0

很酷。 yup優化和異常處理我想:) – DotNetWala 2010-03-10 17:11:35

1

使用XPath這樣的事情:

count(vendor/Slot/Port[@Type='NIC']/Client[@PartitionName='AIX']) 

但是你必須修改它來支持您的命名空間。

[編輯 - 完整的代碼,因爲有人決定下來投票這一點 - 嘆息]

比上年LINQ的路線這種特殊情況下也更容易和更短的代碼。

XmlDocument doc = new XmlDocument(); 
doc.LoadXml(xml); 
XmlNamespaceManager nsMgr = new XmlNamespaceManager(doc.NameTable); 
nsMgr.AddNamespace("inv", "http://secon.com/Ultravendor"); 
int count = doc.SelectNodes("inv:vendor/inv:Slot/inv:Port[@Type='NIC']/inv:Client[@PartitionName='AIX']", nsMgr).Count; 
+0

你能給我完整的代碼,以便我可以學習 – peter 2010-03-10 16:05:11

+0

認真地,下來投票?答案顯然是指向使用常規xpath的解決方案的指針。計數部分是xslt,但仍然是。 – 2010-03-10 18:19:57

3

這裏是一個快速soln我到達使用LINQ。希望你覺得它有用。

static void Main(string[] args) 
    { 
     XElement xElement = XElement.Load(@"C:\Labs\test.xml"); 

     // PartitionName="AIX" is belonging to Type="NIC" 
     var count = xElement.Descendants().Where(x => x.Name.ToString().Contains("Port")) // namespaces might be used here for faster traversal.. 
        .Where(x => x.HasAttributes && x.Attribute("Type").Value == "NIC") 
        .Descendants().Where(x => x.Name.ToString().Contains("Client")) 
        .Where(x => x.Attribute("PartitionName").Value == "AIX").Count();  


     string str = count.ToString(); 

     Console.WriteLine("Count = {0}", str); 
     Console.ReadLine();    

    } 
+0

但在這裏使用linq概念我beleive ,,但我工作在visual studio2005,框架2.o – peter 2010-03-10 16:31:00

+0

這將工作我認爲,但我不認爲這樣它會爲框架2.o – peter 2010-03-10 16:37:24

+0

這將只與工作。淨3.0及以上。我會嘗試使用System.Xml soln來打... – DotNetWala 2010-03-10 16:44:47