简体   繁体   中英

append multiple datasets with a foreach loop in an xml document

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.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");

            }
        }
    }
}

Original Xml File:

<?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 Files for ftProtect (example):

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

Try following:

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

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM