C# 在xml文档中使用foreach循环附加多个数据集

C# 在xml文档中使用foreach循环附加多个数据集,c#,xml,unity3d,C#,Xml,Unity3d,我在用C#for Unity编程。 我的目标是根据属性(“ftProtect”、“ftWarn”、“ftWarn2”)在XML文件中查找某些节点,创建新文件并将数据集(内容)放入其中。 对于每个属性,所有数据集(Name=“Feldsatz1”/Name=“Feldsatz2”)都应该在同一个文件中,但我的代码只是将第一个数据集放在其中 using System.Collections; using System.Collections.Generic; using UnityEngine; u

我在用C#for 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");

            }
        }
    }
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用System.Xml;
使用UnityEngine.UI;
使用制度;
使用System.Xml.Linq;
公共类XML_divide:单行为
{
//在第一帧更新之前调用Start
void Start()
{
//加载XML文件
TextAsset txtXmlAsset=Resources.Load(“Feldsatz”);
//文本的新文档
XmlDocument doc=新的XmlDocument();
//交课文
doc.LoadXml(txtXmlAsset.text);
XmlNodeList xnList=doc.SelectNodes(“/AreaList/Area/FieldList/Field[@Type]”);
var Feldsatznumer_ftProtect=0;
var Feldsatznumer_ftWarn=0;
var Feldsatznumer_ftWarn2=0;
foreach(xnList中的XMLNode1节点)
{
if(node1.Attributes[“Type”].Value==“ftProtect”)
{
var innerXml_ftProtect=node1.innerXml;
Log(innerXml\u ftProtect);
Feldsatznumer_ftProtect=Feldsatznumer_ftProtect+1;
XmlDocument doc_save=新建XmlDocument();
doc_save.LoadXml(innerXml_ftProtect);
doc_save.save(@“C:\Users\micha\Desktop\Thesis\Unity\ganzes Projekt_2\Assets\Resources\ftProtect”+“.xml”);
}
}
foreach(xnList中的XmlNode节点2)
{
if(node2.Attributes[“Type”].Value==“ftWarn”)
{
var innerXml_ftWarn=node2.innerXml;
Log(innerXMLftwarn);
Feldsatznumer_ftWarn=Feldsatznumer_ftWarn+1;
XmlDocument doc_save=新建XmlDocument();
doc_save.LoadXml(innerXml_ftWarn);
doc_save.save(@“C:\Users\micha\Desktop\Thesis\Unity\ganzes Projekt_2\Assets\Resources\ftWarn”+“.xml”);
}
}
foreach(xnList中的XmlNode节点3)
{
if(node3.Attributes[“Type”].Value==“ftWarn2”)
{
var innerXml_ftWarn2=node3.innerXml;
Log(innerXml_ftWarn2);
feldsatznummer_ftWarn2=feldsatznummer_ftWarn2+1;
XmlDocument doc_save=新建XmlDocument();
doc_save.LoadXml(innerXml_ftWarn2);
doc_save.save(@“C:\Users\micha\Desktop\Thesis\Unity\ganzes Projekt_2\Assets\Resources\ftWarn2”+“+feldsatznumer\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的输出文件(示例):


尝试以下操作:

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

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
const string OUTPUT_FOLDER=@“C:\Users\micha\Desktop\Thesis\Unity\ganzes Projekt_2\Assets\Resources\”;
常量字符串标识符=”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
XDocument newDoc=null;
列表区域=doc.substands(“区域”).ToList();
foreach(区域中的像素区域)
{
字符串CoordinateType=(字符串)area.Attribute(“CoordinateType”);
字符串名称=(字符串)area.Attribute(“名称”);
字符串索引=(字符串)area.Attribute(“索引”);
变量组=区域子体(“字段”)
.GroupBy(x=>(字符串)x.Attribute(“Type”)).ToList();
foreach(组中的var组)
{
字符串类型=group.Key;
XElement字段=新XElement(“字段”,新XAttribute(“类型”),类型);
newDoc=XDocument.Parse(IDENT+field.ToString());
XElement root=newDoc.root;
foreach(group.substands中的XElement userPointList(“userPointList”))
{
添加(userPointList);
}
字符串文件名=string.Format(“{0}{1}{2}{3}.xml”,坐标类型,名称,索引,类型);
newDoc.Save(@“c:\temp\test1.xml”);//用于测试
//实际的
//newDoc.Save(输出文件夹+文件名);
}
}
}
}
}

感谢您的帮助,尽管第一个foreach循环在一个循环后退出,但应该有两个循环。我没有收到任何错误,但是每个新文件都只包含第一个datasetEach部分,将被放入不同的文件中。如果您是正确的,那么应该有两个区域:列表区域=doc.substands(“区域”).ToList();如果我打印
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);
                }
            }

        }
    }
}