简体   繁体   English

在C#中读取复杂的XML文件(Unity3d)

[英]Reading a complex XML file in C# (Unity3d)

I'm having big trouble reading an XML Document with c# in Unity. 我在Unity中使用c#读取XML文档时遇到了很大的麻烦。 The XML I'm trying to read has a structure like this: 我尝试读取的XML具有如下结构:

<Classes A>
 <Class A></Class A>
 <Class A></Class A>
  <Class A></Class A>
   <Class A>
    <Detail X>
    <Detail Y>
   </Class A>
   <Class A>
   (...)

Sometimes, Class A has children; 有时,A级有孩子; sometimes, Class A is a child of another Class A etc. 有时,A类是另一个A类的孩子,等等。

I've been trying and researching for 2 days, however I can't manage to understand how to read this using System.XML or System.XML.Linq 我已经尝试和研究了2天,但是我无法理解如何使用System.XML或System.XML.Linq进行阅读

I'm able to read all the Elements and their Attributes using XDocument.Read(), however I'm not able to get any information on whether they're inherited by another class or have children themselves. 我可以使用XDocument.Read()读取所有的Elements及其属性,但是我无法获得有关它们是由另一个类继承还是自己有孩子的任何信息。 How can I read an Element and then have it check whether there are any children and if 我如何阅读一个元素然后让它检查是否有任何孩子以及是否

a) there are children, continue to read the children a)有孩子,继续阅读孩子

or 要么

b) there are no children, continue to read the next Element. b)没有孩子,继续阅读下一个元素。

I understand that XNodes presumably contain the information I want. 我了解XNode大概包含我想要的信息。 I'm able to read all of the XNodes, too. 我也能够阅读所有XNode。 But I'm not able to cast any XNode to an XElement or vice versa. 但是我无法将任何XNode强制转换为XElement,反之亦然。 None of the functions behave as I'd expect. 这些功能均未达到我的预期。

Sample XML 样本XML

<?xml version="1.0" encoding="utf-8" ?>
<Classes A="1">
  <Class A="1.1"/>
  <Class A="1.2">
    <Detail X="1.2.1"/>
  </Class>
  <Class A="1.3">
    <Detail X="1.3.1"/>
    <Detail Y="1.3.2"/>
  </Class>
  <Class A="1.4"/>
</Classes>

C# Code C#代码

        public void XMLToTreeView()
        {
            var reader = XmlReader.Create(@"Path\Sample.xml");
            var xElement = XElement.Load(reader);
            reader.Close();
            findAllNodes(xElement, treeView1);
        }

        private void findAllNodes(XElement xElement, TreeView treeView)
        {
            TreeNode ParentNode = treeView.Nodes.Add(xElement.Attributes().FirstOrDefault().Value);
            foreach (XElement childElement in xElement.Elements())
            {
                TreeNode node = new TreeNode();
                node.Text = childElement.Attributes().FirstOrDefault().Value;
                ParentNode.Nodes.Add(node);
                findAllNodes(childElement, node);
            }
        }

        private void findAllNodes(XElement xElement, TreeNode node)
        {
            foreach (XElement childElement in xElement.Elements())
            {
                TreeNode childNode = new TreeNode();
                childNode.Text = childElement.Attributes().FirstOrDefault().Value;
                node.Nodes.Add(childNode);
                findAllNodes(childElement, childNode);
            }
        }

在此处输入图片说明

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

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