[英]append multiple datasets with a foreach loop in an xml document
我正在為 Unity 使用 C# 編程。 我的目標是根據屬性(“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>
Output 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>
嘗試以下操作:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
const string OUTPUT_FOLDER = @"C:\Users\micha\Desktop\Thesis\Unity\ganzes Projekt_2\Assets\Resources\";
const string IDENT = "<?xml version=\"1.0\"?>";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XDocument newDoc = null;
List<XElement> areas = doc.Descendants("Area").ToList();
foreach (XElement area in areas)
{
string coordinatesType = (string)area.Attribute("CoordinatesType");
string name = (string)area.Attribute("Name");
string index = (string)area.Attribute("Index");
var groups = area.Descendants("Field")
.GroupBy(x => (string)x.Attribute("Type")).ToList();
foreach (var group in groups)
{
string type = group.Key;
XElement field = new XElement("Field", new XAttribute("Type", type));
newDoc = XDocument.Parse(IDENT + field.ToString());
XElement root = newDoc.Root;
foreach (XElement userPointList in group.Descendants("UserPointList"))
{
root.Add(userPointList);
}
string filename = string.Format("{0}_{1}_{2}_{3}.xml", coordinatesType, name, index, type);
newDoc.Save(@"c:\temp\test1.xml"); //for testing
//actual
//newDoc.Save(OUTPUT_FOLDER + filename);
}
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.