簡體   English   中英

從 XML 文檔中讀取值到 Int 數組 c#

[英]Reading values from XML document into an Int Array c#

我無法在任何我能正確理解的地方找到合適的解決方案。 因此,如果您能解釋該怎么做,那就太好了。 我正在編寫一個程序,可以從視頻游戲中的保存文件中讀取數據。

我怎樣才能在 C# 中寫一些東西,將專業下的整數保存到一個數組中? 我現在正在使用 XDocument 和一個指向“播放器”元素作為根的變量。

var player = doc.Root.Element("player");

我最初的想法是創建一個以職業為根的變量,並使用forloop將職業中元素的所有值保存到一個數組中,但我不知道如何開始

對於上下文,這就是 XML 的樣子(我添加了一些句點來表明還有其他元素):

<?xml version="1.0" encoding="utf-8"?>
<SaveGame xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <player>
        .
        .
        .
        <professions>
            <int>1</int>
            <int>18</int>
            <int>25</int>
            <int>13</int>
            <int>4</int>
            <int>6</int>
        </professions>
        .
        .
        .
    </player>
</SaveGame>

使用 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
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            List <Player> players = new List<Player>() {
                new Player() { professions = new List<Profession>() {
                    new Profession() { data = new int[]{100, 200, 300, 400}},
                    new Profession() { data = new int[]{1100, 1200, 1300, 1400}},
                    new Profession() { data = new int[]{2100, 2200, 2300, 2400}}
                }},
                new Player() { professions = new List<Profession>() {
                    new Profession() { data = new int[]{3100, 3200, 3300, 3400}},
                    new Profession() { data = new int[]{3100, 3200, 3300, 3400}},
                    new Profession() { data = new int[]{4100, 4200, 4300, 4400}}
                }}
            };



            string ident = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                "<SaveGame xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">" +
                "</SaveGame>";

            XDocument doc = XDocument.Parse(ident);
            XElement saveGame = doc.Root;

            foreach (Player player in players)
            {
                XElement newPlayer = new XElement("player");
                saveGame.Add(newPlayer);
                foreach (Profession profession in player.professions)
                {
                    newPlayer.Add(new XElement("professions", profession.data.Select(x => new XElement("int", x))));
                }
            }
            doc.Save(FILENAME);

        }
    }
    public class Player
    {
        public List<Profession> professions { get; set; }
    }
    public class Profession
    {
        public int[] data { get; set; }
    }
}

您也可以使用 xml 串行器

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            SaveGame saveGame = new SaveGame();
            saveGame.players = new List<Player>() {
                new Player() { professions = new List<Profession>() {
                    new Profession() { data = new int[]{100, 200, 300, 400}},
                    new Profession() { data = new int[]{1100, 1200, 1300, 1400}},
                    new Profession() { data = new int[]{2100, 2200, 2300, 2400}}
                }},
                new Player() { professions = new List<Profession>() {
                    new Profession() { data = new int[]{3100, 3200, 3300, 3400}},
                    new Profession() { data = new int[]{3100, 3200, 3300, 3400}},
                    new Profession() { data = new int[]{4100, 4200, 4300, 4400}}
                }}
            };

            XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
            ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            ns.Add("xsd", "http://www.w3.org/2001/XMLSchema");

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;

            XmlWriter writer = XmlWriter.Create(FILENAME, settings);
            XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));

            serializer.Serialize(writer, saveGame, ns);

        }
    }
    public class SaveGame
    {
        [XmlElement("player")]
        public List<Player> players { get; set; }

    }
    public class Player
    {
        [XmlElement("profession")]
        public List<Profession> professions { get; set; }
    }
    public class Profession
    {
        [XmlElement("int")]
        public int[] data { get; set; }
    }
}

下面的代碼反序列化 xml 文件

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
            SaveGame sameGame = (SaveGame)serializer.Deserialize(reader);
        }
    }
    public class SaveGame
    {
        [XmlElement("player")]
        public List<Player> players { get; set; }

    }
    public class Player
    {
        [XmlElement("profession")]
        public List<Profession> professions { get; set; }
    }
    public class Profession
    {
        [XmlElement("int")]
        public int[] data { get; set; }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM