Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#-解析xml节点_C#_Xml - Fatal编程技术网

C#-解析xml节点

C#-解析xml节点,c#,xml,C#,Xml,我使用C#从XML加载数据的方式如下: XmlDocument xmlDoc = new XmlDocument(); TextAsset xmlFile = Resources.Load("levels/" + levelID) as TextAsset; xmlDoc.LoadXml(xmlFile.text); XmlNodeList levelsList = xmlDoc.GetElementsByTagName("level"); foreach (XmlNode levelInf

我使用C#从XML加载数据的方式如下:

XmlDocument xmlDoc = new XmlDocument();
TextAsset xmlFile = Resources.Load("levels/" + levelID) as TextAsset;
xmlDoc.LoadXml(xmlFile.text);

XmlNodeList levelsList = xmlDoc.GetElementsByTagName("level");

foreach (XmlNode levelInfo in levelsList)
{
    XmlNodeList childNodes = levelInfo.ChildNodes;

    foreach (XmlNode value in childNodes)
    {
        switch (value.Name)
        {
            case "info":
                //levelWidth = getInt(value, 0);
                //levelHeight = getInt(value, 1);
                break;
        }
    }
}
下面是我正在加载的XML:

<?xml version="1.0" encoding="utf-8" ?>
<level>
  <info w="1000" h="500"/>
</level>

它工作得很好,我现在正试图找到加载子节点的最佳方法,在我的级别节点中,有多个节点

<?xml version="1.0" encoding="utf-8" ?>
<level>
    <info w="1000" h="500"/>
    <ground>
      <point val1="val1" val2="val2"/>
    </ground>
</level>


如果您能给我一些指导,告诉我如何朝正确的方向前进,我将不胜感激。

如果您需要阅读所有要点,您可以使用

var nodeList = Xmldocument.SelectNodes("level/info/ground/point");

选择节点返回节点列表

我会选择一种完全不同的方式,使用数据对象。然后,您不必分析xml,只需编写数据类代码:

[Serializable()]
public class CLevel
{
    public string Info { get; set; }
}

[Serializable()]
public class CDatafile
{
    public List<CLevel> LevelList { get; set; }

    public CDatafile()
    {
        LevelList = new List<CLevel>();
    }
}

public class DataManager
{
    private string FileName = "Data.xml";
    public CDatafile Datafile { get; set; }

    public DataManager()
    {
        Datafile = new CDatafile();
    }

    // Load file
    public void LoadFile()
    {
        if (System.IO.File.Exists(FileName))
        {
            System.IO.StreamReader srReader = System.IO.File.OpenText(FileName);
            Type tType = Datafile.GetType();
            System.Xml.Serialization.XmlSerializer xsSerializer = new System.Xml.Serialization.XmlSerializer(tType);
            object oData = xsSerializer.Deserialize(srReader);
            Datafile = (CDatafile)oData;
            srReader.Close();
        }
    }

    // Save file
    public void SaveFile()
    {
        System.IO.StreamWriter swWriter = System.IO.File.CreateText(FileName);
        Type tType = Datafile.GetType();
        if (tType.IsSerializable)
        {
            System.Xml.Serialization.XmlSerializer xsSerializer = new System.Xml.Serialization.XmlSerializer(tType);
            xsSerializer.Serialize(swWriter, Datafile);
            swWriter.Close();
        }
    }
因此,您可以在编译器检查的代码中执行所有操作。让生活变得更简单,或者你怎么想?

使用XML Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
             "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<level>" +
                "<info w=\"1000\" h=\"500\"/>" +
            "</level>";

            XDocument doc = XDocument.Parse(xml);

            XElement level = (XElement)doc.FirstNode;

            level.Add("ground", new object[] {
                new XElement("point", new XAttribute[] {
                    new XAttribute("val1", "val1"),
                    new XAttribute("val2", "val2")
                })
            });
        }
    }
}
​
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串xml=
"" +
"" +
"" +
"";
XDocument doc=XDocument.Parse(xml);
XElement级别=(XElement)doc.FirstNode;
标高。添加(“地面”,新对象[]{
新XElement(“点”,新XAttribute[]{
新XAttribute(“val1”、“val1”),
新XAttribute(“val2”、“val2”)
})
});
}
}
}
​

首先,如果可能的话,我强烈建议您转向使用LINQ to XML。无论从哪个方面看,它都要好得多。接下来,我将寻找特定的元素并适当地处理它们,而不是读取所有节点,然后打开它们是什么。如果一切都很好,你想改进,那么你需要尝试CodeReview你需要阅读所有要点吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
             "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<level>" +
                "<info w=\"1000\" h=\"500\"/>" +
            "</level>";

            XDocument doc = XDocument.Parse(xml);

            XElement level = (XElement)doc.FirstNode;

            level.Add("ground", new object[] {
                new XElement("point", new XAttribute[] {
                    new XAttribute("val1", "val1"),
                    new XAttribute("val2", "val2")
                })
            });
        }
    }
}
​