C# - 解析xml节点

时间:2015-11-30 20:11:27

标签: 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 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>

我将非常感谢如何朝着正确的方向前进,谢谢。

3 个答案:

答案 0 :(得分:1)

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

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

SelectNodes返回节点列表。

答案 1 :(得分:1)

使用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")
                })
            });
        }
    }
}
​

答案 2 :(得分:0)

我会采用一种不同的方式并使用数据对象。那么你不必分析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();
        }
    }

然后您可以使用它来创建,保存和加载文件,如下所示:

DataManager dataMng = new DataManager();

// Create some data
CLevel level = new CLevel();
level.Info = "Testlevel";
dataMng.Datafile.LevelList.Add(level);

// Save to file
dataMng.SaveFile();

// Load from file
dataMng.LoadFile();

因此,您可以在编译器检查的代码中执行所有操作。让生活变得更轻松,或者您的想法是什么?