本文介绍了我如何解析 svg 的 xml 文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 ()
    {

    }
}

我想得到

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

Then to parse each child to array for example:

<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;

So i want to create array call it RectsMaybe string[] Rects;

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

Then under each Rect in the array to have his parameters also as strings:

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

这种格式.

这样我就可以从另一个脚本访问 Rect1 及其参数,例如:Rect1.width... 或 Rect1.x....Rect1.fill....

So then i can get access from another script to Rect1 and his parameters like:Rect1.width... or Rect1.x....Rect1.fill....

推荐答案

就这样试试,

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>

这篇关于我如何解析 svg 的 xml 文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 18:06