将XML文档中的值读入Int数组c#

问题描述 投票:0回答:1

我无法正确理解的任何地方都找不到合适的解决方案。因此,如果您可以解释该怎么做,那将很棒。我正在编写一个程序,可以从视频游戏中的保存文件中读取数据。

我如何用C#编写将某些专业下的整数保存到数组中的内容?我现在正在使用XDocument,其变量指向根的“ player”元素。

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>
c# xml linq-to-xml
1个回答
0
投票

使用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; }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.