简体   繁体   English

append 在 xml 文档中具有 foreach 循环的多个数据集

[英]append multiple datasets with a foreach loop in an xml document

I'm programming in C# for Unity.我正在为 Unity 使用 C# 编程。 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.我的目标是根据属性(“ftProtect”、“ftWarn”、“ftWarn2”)在我的 XML 文件中找到某些节点,创建新文件并将数据集(内容)放在那里。 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.对于每个属性,所有数据集(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");

            }
        }
    }
}

Original Xml File:原装 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 Files for ftProtect (example): 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>

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

        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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