Hi,
I'm programming in C# for Unity.
My goal is to find certain nodes in my XML file based on the attribute("ftProtect", "ftWarn","ftWarn2"), create new files and put the datasets (content) in there.
For each attribute all datasets (Name="Feldsatz1"/Name="Feldsatz2") should be in the same file, but my code is just putting the first dataset in there.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using UnityEngine;
public class XML_divide_new : MonoBehaviour
{
void Start()
{
const string FILENAME = @"C:\Users\micha\Desktop\Thesis\Unity\ganzes Projekt_2\Assets\Resources\Feldsatz.xml";
const string OUTPUT_FOLDER = @"C:\Users\micha\Desktop\Thesis\Unity\ganzes Projekt_2\Assets\Resources\";
const string IDENT = "<?xml version=\"1.0\"?>";
XDocument doc = XDocument.Load(FILENAME);
XDocument newDoc = new XDocument();
List<XElement> areas = doc.Descendants("Area").ToList();
List<XElement> content = new List<XElement>();
foreach (XElement area in areas)
{
foreach (XElement field in area.Descendants("Field"))
{
string type = (string)field.Attribute("Type");
string filename = string.Format("{0}.xml", type);
XElement userPointList = field.Element("UserPointList");
newDoc = XDocument.Parse(IDENT + userPointList.ToString());
newDoc.Save(OUTPUT_FOLDER + filename);
}
}
}
}
Original XML File:
="1.0"="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>
Desired Output List (example 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>
What I have tried:
I tried implementing a List (content) to store the Data before Saving it. But by that i produced the error "Data at the root level is invalid. Line1 Position 1"
using System;
...
List<XElement> content = new List<XElement>();
foreach (XElement area in areas)
foreach (XElement field in area.Descendants("Field"))
{
content.Add(area);
content.Add(userPointList);
newDoc = XDocument.Parse(content.ToString());
newDoc.Save(OUTPUT_FOLDER + filename);
}