2013-10-23 56 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