提问者:小点点

如何使节点成为ParentNode?


我正在从xml文件中提取数据集,以便在新文件中对其进行排序。为此,我需要使节点成为的父节点(以便能够根据属性feldsatz=读出值)。我试了几件事,但都没能正常工作。我的项目是基于C#for Unity3D的。如果有人给我推荐,那就太棒了。

预期结果:

<?xml version="1.0" encoding="UTF-8"?>
-<Field Type="ftProtect">  
   <Area Feldsatz="1">
    -<UserPointList>
       <UserPoint Contour="FALSE" Distance="57" Angle="0"/>
       ...
    </UserPointList>
  <Area/>
  <Area Feldsatz="2"/>
    -<UserPointList>
       <UserPoint Contour="FALSE" Distance="57" Angle="0"/>
       ...
     </UserPointList>
   <Area/>   
 </Field>

实际结果:

<?xml version="1.0" encoding="UTF-8"?>
-<Field Type="ftProtect">
  <Area Feldsatz="1"/>
  -<UserPointList>
    <UserPoint Contour="FALSE" Distance="57" Angle="0"/>
    ..
  </UserPointList>
  <Area Feldsatz="2"/>
  -<UserPointList>
    <UserPoint Contour="FALSE" Distance="57" Angle="0"/>
    ...
  </UserPointList>
</Field>

代码:

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()
    {
        //Definition of Parameters
        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 = null;
        //List for the "Feldlisten"
        List<XElement> areas = doc.Descendants("Area").ToList();
        //Counter for the "Feldlisten"
        int counter = 1;
        
        //Grouping of Fieldtypes
        var groups = areas.Descendants("Field").GroupBy(x => (string)x.Attribute("Type")).ToList();

        foreach (var group in groups)
        {
            string type = group.Key;
            //Introduce the field node
            XElement field = new XElement("Field", new XAttribute("Type", type));
            //Implement Header and Ident 
            newDoc = XDocument.Parse(IDENT + field.ToString());
            XElement root = newDoc.Root;

            foreach (XElement userPointList in group.Descendants("UserPointList"))
            {
                //Introduce the area node
                XElement number = new XElement("Area", new XAttribute("Feldsatz", counter));

                //Add "Feldsatznummer" and Datasets; divided by Indexnumbers
                root.Add(number);
                root.Add(userPointList);
                counter = counter + 1;
            }

            //Zero Counter
            counter = 1;
            //Filename
            string filename = string.Format("{0}.xml", type);
            //Save File
            newDoc.Save(OUTPUT_FOLDER + filename);
        }
    }
}

原始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"/>
       ...
      </UserPointList>
     </Field>
     -<Field Type="ftWarn">
      -<UserPointList>
        <UserPoint Contour="FALSE" Distance="71" Angle="0"/>
        ...
       </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"/>
        ...
       </UserPointList>
      </Field>
      -<Field Type="ftWarn">
       -<UserPointList>
        <UserPoint Contour="FALSE" Distance="71" Angle="0"/>
        ...
       </UserPointList>
      </Field>
      <Field Type="ftWarn2"/>
     </FieldList>
   </Area>
 </AreaList>

共1个答案

匿名用户

在我看来,您当前似乎是在添加创建的空area元素number,该元素只包含属性,然后将userpointlist添加到中的相同root级别(字段)

//Introduce the area node
XElement number = new XElement("Area", new XAttribute("Feldsatz", counter));

//Add "Feldsatznummer" and Datasets; divided by Indexnumbers
root.Add(number);
root.Add(userPointList);

如果我理解正确的话,我们的目标是再次将它们嵌套在一起,就像

//Introduce the area node
XElement number = new XElement("Area", new XAttribute("Feldsatz", counter));

// Add the UserPointList nested under the Area element
number.Add(userPointList);

//Add "Feldsatznummer" and Datasets; divided by Indexnumbers
root.Add(number);