How can i parse xml file type of svg? [duplicate]

浪尽此生 提交于 2021-01-28 07:02:20

问题


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

    }
}

I want to get all the children under <g>

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" />

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

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"

This format.

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


回答1:


In this case rather use LINK to XML is way flexible

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:


Just try like this,

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

Sample SVG File,

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


来源:https://stackoverflow.com/questions/44332451/how-can-i-parse-xml-file-type-of-svg

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!