我正在用 C# 为 Unity 编程。我的目标是根据属性(“ftProtect”、“ftWarn”、“ftWarn2”)在我的 XML 文件中找到某些节点,创建新文件并将数据集(内容)放在那里。对于每个属性,所有数据集(Name="Feldsatz1"/Name="Feldsatz2")都应该在同一个文件中,但我的代码只是将第一个数据集放在那里。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml;
using UnityEngine.UI;
using System;
using System.Xml.Linq;
public class XML_divide : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
//Load XML - File
TextAsset txtXmlAsset = Resources.Load<TextAsset>("Feldsatz");
//New Doc for the Text
XmlDocument doc = new XmlDocument();
//Pass in the Text
doc.LoadXml(txtXmlAsset.text);
XmlNodeList xnList = doc.SelectNodes("/AreaList/Area/FieldList/Field[@Type]");
var feldsatznummer_ftProtect = 0;
var feldsatznummer_ftWarn = 0;
var feldsatznummer_ftWarn2 = 0;
foreach (XmlNode node1 in xnList)
{
if (node1.Attributes["Type"].Value == "ftProtect")
{
var innerXml_ftProtect = node1.InnerXml;
Debug.Log(innerXml_ftProtect);
feldsatznummer_ftProtect = feldsatznummer_ftProtect + 1;
XmlDocument doc_save = new XmlDocument();
doc_save.LoadXml(innerXml_ftProtect);
doc_save.Save(@"C:\Users\micha\Desktop\Thesis\Unity\ganzes Projekt_2\Assets\Resources\ftProtect" + "_" + feldsatznummer_ftProtect + ".xml");
}
}
foreach (XmlNode node2 in xnList)
{
if (node2.Attributes["Type"].Value == "ftWarn")
{
var innerXml_ftWarn = node2.InnerXml;
Debug.Log(innerXml_ftWarn);
feldsatznummer_ftWarn = feldsatznummer_ftWarn + 1;
XmlDocument doc_save = new XmlDocument();
doc_save.LoadXml(innerXml_ftWarn);
doc_save.Save(@"C:\Users\micha\Desktop\Thesis\Unity\ganzes Projekt_2\Assets\Resources\ftWarn" + "_" + feldsatznummer_ftWarn + ".xml");
}
}
foreach (XmlNode node3 in xnList)
{
if (node3.Attributes["Type"].Value == "ftWarn2")
{
var innerXml_ftWarn2 = node3.InnerXml;
Debug.Log(innerXml_ftWarn2);
feldsatznummer_ftWarn2 = feldsatznummer_ftWarn2 + 1;
XmlDocument doc_save = new XmlDocument();
doc_save.LoadXml(innerXml_ftWarn2);
doc_save.Save(@"C:\Users\micha\Desktop\Thesis\Unity\ganzes Projekt_2\Assets\Resources\ftWarn2" + "_" + feldsatznummer_ftWarn2 + ".xml");
}
}
}
}
原始 Xml 文件:
<?xml version="1.0" encoding="WINDOWS-1252"?>
<AreaList DeviceType="sctS300" FieldIntrusion="triple" Resolution="0,5">
<Area CoordinatesType="polar" Name="Feldsatz 1" Index="0">
<FieldList>
<Field Type="ftProtect">
<UserPointList>
<UserPoint Contour="FALSE" Distance="57" Angle="0" />
<UserPoint Contour="FALSE" Distance="50" Angle="81,5" />
<UserPoint Contour="FALSE" Distance="50" Angle="187,5" />
<UserPoint Contour="FALSE" Distance="56" Angle="270" />
</UserPointList>
</Field>
<Field Type="ftWarn">
<UserPointList>
<UserPoint Contour="FALSE" Distance="71" Angle="0" />
<UserPoint Contour="FALSE" Distance="64" Angle="83,5" />
<UserPoint Contour="FALSE" Distance="64" Angle="186" />
<UserPoint Contour="FALSE" Distance="71" Angle="270" />
</UserPointList>
</Field>
<Field Type="ftWarn2" />
</FieldList>
</Area>
<Area CoordinatesType="polar" Name="Feldsatz 2" Index="1">
<FieldList>
<Field Type="ftProtect">
<UserPointList>
<UserPoint Contour="FALSE" Distance="57" Angle="0" />
<UserPoint Contour="FALSE" Distance="50" Angle="81,5" />
<UserPoint Contour="FALSE" Distance="32" Angle="115,5" />
<UserPoint Contour="FALSE" Distance="80" Angle="128" />
<UserPoint Contour="FALSE" Distance="80" Angle="142,5" />
<UserPoint Contour="FALSE" Distance="32" Angle="155" />
<UserPoint Contour="FALSE" Distance="50" Angle="187,5" />
<UserPoint Contour="FALSE" Distance="56" Angle="270" />
</UserPointList>
</Field>
<Field Type="ftWarn">
<UserPointList>
<UserPoint Contour="FALSE" Distance="71" Angle="0" />
<UserPoint Contour="FALSE" Distance="64" Angle="83,5" />
<UserPoint Contour="FALSE" Distance="64" Angle="186" />
<UserPoint Contour="FALSE" Distance="71" Angle="270" />
</UserPointList>
</Field>
<Field Type="ftWarn2" />
</FieldList>
</Area>
</AreaList>
ftProtect 的输出文件(示例):
<?xml version="1.0"?>
<Area CoordinatesType="polar" Name="Feldsatz 1" Index="1">
<UserPointList>
<UserPoint Contour="FALSE" Distance="57" Angle="0" />
<UserPoint Contour="FALSE" Distance="50" Angle="81,5" />
<UserPoint Contour="FALSE" Distance="50" Angle="187,5" />
<UserPoint Contour="FALSE" Distance="56" Angle="270" />
</UserPointList>
<Area CoordinatesType="polar" Name="Feldsatz 2" Index="1">
<UserPointList>
<UserPoint Contour="FALSE" Distance="57" Angle="0" />
<UserPoint Contour="FALSE" Distance="50" Angle="81,5" />
<UserPoint Contour="FALSE" Distance="32" Angle="115,5" />
<UserPoint Contour="FALSE" Distance="80" Angle="128" />
<UserPoint Contour="FALSE" Distance="80" Angle="142,5" />
<UserPoint Contour="FALSE" Distance="32" Angle="155" />
<UserPoint Contour="FALSE" Distance="50" Angle="187,5" />
<UserPoint Contour="FALSE" Distance="56" Angle="270" />
</UserPointList>