【问题标题】:How can i parse xml file type of svg? [duplicate]我如何解析 svg 的 xml 文件类型? [复制]
【发布时间】:2017-11-04 01:45:08
【问题描述】:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;

public class XmlReader : MonoBehaviour
{
    XmlDocument doc = new XmlDocument();

    // Use this for initialization
    void Start ()
    {
        doc.Load(@"C:\Users\myxml\Documents\mysvg.svg");
        XmlNode node = doc.DocumentElement.SelectSingleNode("/g");

        foreach (XmlNode nodes in doc.DocumentElement.ChildNodes)
        {
            string text = nodes.InnerText; //or loop through its children as well
        }
    }

    // Update is called once per frame
    void Update ()
    {

    }
}

我想得到<g>下的所有孩子

然后将每个孩子解析为数组,例如:

<g
     inkscape:label="Layer 1"
     inkscape:groupmode="layer"
     id="layer1">
    <rect
       style="opacity:1;fill:#00c8fc;fill-opacity:0.98823529;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
       id="rect4155"
       width="45.714287"
       height="30"
       x="37.387959"
       y="115.30345" />
    <rect
       style="opacity:1;fill:#00c8fc;fill-opacity:0.98823529;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
       id="rect4155-5"
       width="45.714287"
       height="30"
       x="91.899246"
       y="115.40621" />

所以我想创建数组调用它Rects 也许string[] Rects;

然后在数组中的每个 Rect 下将他的参数也作为字符串:

Rect1
 fill:#00c8fc
 width="45.714287"
 height="30"
 x="37.387959"
 y="115.30345"

这种格式。

然后我可以从另一个脚本访问 Rect1 和他的参数,例如: Rect1.width... 或 Rect1.x....Rect1.fill....

【问题讨论】:

  • 不是 svg html 而不是 xml?
  • 答案是,您可以像解析任何其他 XML 文件一样解析它。这里有几十个类似的问题。

标签: c# xml svg xml-parsing


【解决方案1】:

在这种情况下,使用LINK to XML 更灵活

XDocument xdoc = XDocument.Load(@"C:\Users\myxml\Documents\mysvg.svg");
var rectElements = xdoc.Descendants("rect");

foreach(var rect in rectElements){
  var attributes = rect.Attributes();
  //Store them in some sort of Data Structure to support your requirements, maybe a Dictionary  
}

【讨论】:

    【解决方案2】:

    就这样试试吧,

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Xml.Linq;
    
    namespace SVGRead
    {
        class Program
        {
            static void Main(string[] args)
            {
                string filePath = AppDomain.CurrentDomain.BaseDirectory + "test.svg";
                XDocument doc = XDocument.Load(filePath);
                XElement rootElements = doc.Root;
                IEnumerable<XElement> nodes = from element1 in rootElements.Elements("{http://www.w3.org/2000/svg}g") select element1;
                foreach (var node in nodes)
                {
                    IEnumerable<XElement> childNodes = from element2 in node.Elements("{http://www.w3.org/2000/svg}rect") select element2;
                    foreach (var childNod in childNodes)
                    {
                        //Get child of <g>, ract tag
                        string txtRect = childNod.ToString();
    
                        //Get Attribute values like "style", "width", "height", etc..
                        string style = childNod.Attribute("style").Value;
                        string width = childNod.Attribute("width").Value;
                        string height = childNod.Attribute("height").Value;
                    }
                }
            }
        }
    }
    

    示例 SVG 文件,

    <?xml version="1.0" encoding="utf-8"?>
    <!-- Generator: Adobe Illustrator 16.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
    <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
    <svg version="1.1" id="Bahrain_Map" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px"
         y="0px" width="1000px" height="1500px" viewBox="0 0 1000 1500" enable-background="new 0 0 1000 1500" xml:space="preserve">
        <g>
            <rect 
           style="opacity:1;fill:#00c8fc;fill-opacity:0.98823529;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
           id="rect4155"
           width="45.714287"
           height="30"
           x="37.387959"
           y="115.30345" />
        <rect
           style="opacity:1;fill:#00c8fc;fill-opacity:0.98823529;fill-rule:nonzero;stroke:none;stroke-width:1;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
           id="rect4155-5"
           width="45.714287"
           height="30"
           x="91.899246"
           y="115.40621" />     
        </g>
    </svg>
    

    【讨论】:

      猜你喜欢
      • 2017-02-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-09
      • 1970-01-01
      • 2015-05-16
      相关资源
      最近更新 更多