【问题标题】:Get label of svg element according to its id根据 id 获取 svg 元素的标签
【发布时间】:2013-11-01 15:59:55
【问题描述】:

我想获取 svg 元素的标签。我写了一个控制台应用程序和 thougt svg 文件作为 xml。我正在尝试根据元素的 id 获取标签。当我在下面编写代码时,我得到了所有 id,但我无法获得该行的标签。我如何访问标签?元素的一个例子是;

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

【问题讨论】:

    标签: c# xml svg xml-parsing


    【解决方案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"
    

    【讨论】:

    • 如果我尝试获取具有类属性的元素,我该如何提供它?
    猜你喜欢
    • 1970-01-01
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-09
    • 2012-05-19
    • 2014-06-12
    相关资源
    最近更新 更多