2014-05-18 37 views
3

我有XMLReader的設置和可以檢查某些元素,但我不能找到一種方法來檢查閉合元件,可以說我希望有在</perls>標籤另一個case語句除了開幕式之一,我怎麼能做到這一點?我知道肯定,這樣的標籤是不是自閉。檢查是否XMLReader就具體的結束元素

using (XmlReader reader = XmlReader.Create("perls.xml")) 
{ 
    while (reader.Read()) 
    { 
    // Only detect start elements. 
    if (reader.IsStartElement()) 
    { 
     // Get element name and switch on it. 
     switch (reader.Name) 
     { 
     case "perls": 
      // Detect this element. 
      Console.WriteLine("Start <perls> element."); 
      break; 
     case "article": 
      // Detect this article element. 
      Console.WriteLine("Start <article> element."); 
      // Search for the attribute name on this current node. 
      string attribute = reader["name"]; 
      if (attribute != null) 
      { 
      Console.WriteLine(" Has attribute name: " + attribute); 
      } 
      // Next read will contain text. 
      if (reader.Read()) 
      { 
      Console.WriteLine(" Text node: " + reader.Value.Trim()); 
      } 
      break; 
     } 
    } 
    } 
} 

回答

相關問題