2013-10-23 59 views
1

我想獲取svg元素的標籤。我編寫了一個控制檯應用程序,並將svg文件作爲xml。我試圖根據元素的id來獲取標籤。當我在下面寫代碼時,我得到了所有的ID,但我無法獲得該行的標籤。我如何訪問標籤?元素的一個例子是;根據其標識獲取svg元素的標籤

<rect 
     style="fill:#cccccc" 
     id="21" 
     width="35.823246" 
     height="35.823246" 
     x="299.87155" 
     y="65.999405" 
     class="seatObj" 
     inkscape:label="A22" /> 

而且代碼是;

class Program 
    { 
     static void Main(string[] args) 
     { 
      XmlDocument doc = new XmlDocument(); 
      doc.Load(@"seatChart-01.svg"); 
      XmlNodeList elementList = doc.GetElementsByTagName("rect"); 
      string[] labels = new string[elementList.Count]; 
      for (int i = 0; i < elementList.Count; i++) 
      { 
       int id = int.Parse(elementList[i].Attributes["id"].Value); 
       labels[id] = elementList[i].Attributes["inkspace:label"].Value;    
      } 
      for (int i = 0; i < labels.Length; i++) 
      { 
       Console.WriteLine(labels[i]); 
      } 
      Console.ReadLine(); 
     } 
    } 

回答

1
elementList[i].Attributes["inkspace:label"].Value 

應該

elementList[i].Attributes["label", "http://www.inkscape.org/namespaces/inkscape"].Value 

我假設的命名空間是,Inkscape的繪圖程序在這裏。要確認看起來像這樣的根元素上的東西...

xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape" 
+0

如果我嘗試獲取類屬性的元素,我該如何提供它? – ebruszl