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
....
答案 0 :(得分:0)
在这种情况下,使用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
}
答案 1 :(得分:0)
就像这样尝试
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>