Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.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#_.net_Xml_Directory - Fatal编程技术网

C# 如何加载多个XML文件并读取它们?

C# 如何加载多个XML文件并读取它们?,c#,.net,xml,directory,C#,.net,Xml,Directory,我有个问题。在我的代码中,我在一个目录中查找所有xml文件。最后它成功了,但我不知道怎么读。其中一个加载了字符串xmlFile。我想我需要以这种方式替换或编辑它,因为xmlFile不仅是一个文件,而且是目录中的所有文件 我该怎么办 namespace WindowsFormsApplication11 { public partial class Form1 : Form { private const string xmlFile = "C:\Games\gam

我有个问题。在我的代码中,我在一个目录中查找所有xml文件。最后它成功了,但我不知道怎么读。其中一个加载了字符串xmlFile。我想我需要以这种方式替换或编辑它,因为xmlFile不仅是一个文件,而且是目录中的所有文件

我该怎么办

namespace WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        private const string xmlFile = "C:\Games\games.xml"; // single xml file works

        public Form1()
        {
            this.InitializeComponent();
            this.InitializeListView();
            this.LoadDataFromXml();
        listView.Items.AddRange(Directory.GetFiles("C:\Games\", "*.xml")
                                 .Select(f => new ListViewItem(f))
                                 .ToArray()); 
        }

        private void LoadDataFromXml()
        {
            if (File.Exists(xmlFile))
            {
                XDocument document = XDocument.Load(xmlFile);
                if (document.Root != null)
                {
                    foreach (XElement gameElement in document.Root.Elements("game"))
                    {
                        string gamename = gameElement.Element("gamename").Value;
                        string launchpath = gameElement.Element("launchpath").Value;
                        string uninstallpath = gameElement.Element("uninstallpath").Value;
                        string publisher = gameElement.Element("publisher").Value;
                        // check if gameElement.Element(ELEMENTNAME) is not null  
                        Game game = new Game(gamename, launchpath, uninstallpath, publisher);
                        AddGameToListView(game);
                    }
                }
            }
        }

        private void AddGameToListView(Game game)
        {
            ListViewItem item = CreateGameListViewItem(game);
            this.listView.Items.Add(item);
        }

        private ListViewItem CreateGameListViewItem(Game game)
        {
            ListViewItem item = new ListViewItem(game.Gamename);
            item.SubItems.Add(game.Launchpath);
            item.SubItems.Add(game.Uninstallpath);
            item.SubItems.Add(game.Publisher);
            item.Tag = game;
            return item;
        }



        private void InitializeListView()
        {
            this.listView.View = View.Details;
            this.listView.GridLines = true;
            this.listView.MultiSelect = false;
            this.listView.FullRowSelect = true;
            this.listView.Columns.AddRange(new[]
                {
                    new ColumnHeader{Text = "Gamename", Width = 200},
                    new ColumnHeader{Text = "Launchpath"},
                    new ColumnHeader{Text = "Uninstallpath"},
                    new ColumnHeader{Text = "Publisher"} 
                });
            this.listView.MouseDoubleClick += ListViewOnMouseDoubleClick;
        }

        private void ListViewOnMouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && this.listView.SelectedItems.Count > 0)
            {
                Game game = (Game)((this.listView.SelectedItems[0].Tag);
                try
                {
                    Process.Start(game.Launchpath);
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Can not start game.\nDetails:\n" + ex.Message, "Error", MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);
                }
            }
        }
    }

    internal class Game
    {
        public Game()
        {

        }

        public Game(string gamename, string launchpath, string uninstallpath, string publisher)
        {
            this.Gamename = gamename;
            this.Launchpath = launchpath;
            this.Uninstallpath = uninstallpath;
            this.Publisher = publisher;
        }
        public string Gamename { get; set; }
        public string Launchpath { get; set; }
        public string Uninstallpath { get; set; }
        public string Publisher { get; set; }
    }
}
更新:

这是我当前的代码。如何将f发送到LoadDataFromXml

      public Form1()
    {
        this.InitializeComponent();
        this.InitializeListView();
        this.Height = Screen.PrimaryScreen.WorkingArea.Height;
        var files = Directory.GetFiles(@"C:\Games\", "*.xml").Select(f => new ListViewItem(f)).ToArray(); listView.Items.AddRange(files);
        foreach (var f in files)
        {
            this.LoadDataFromXml(f);
        }

    }

    private void LoadDataFromXml(//What Do I need to enter here?)
    {


               foreach (XElement gameElement in f.Root.Elements("game"))
                {
                    string gamename = gameElement.Element("gamename").Value;
                    string launchpath = gameElement.Element("launchpath").Value;
                    string portablesave = gameElement.Element("portablesave").Value;
                    string publisher = gameElement.Element("publisher").Value;
                    string gameid = gameElement.Element("gameID").Value;
                    string update = gameElement.Element("update").Value;
                    // check if gameElement.Element(ELEMENTNAME) is not null  
                    Game game = new Game(gamename, launchpath, portablesave, publisher, gameid, update);
                    AddGameToListView(game);
                }


    }

简单地使用目录函数获取所有XML文件,并通过为每个文件调用
LoadDataFromXml
循环遍历它们。注意:您需要稍微重构一下代码

您需要修改
LoadDataFromXml
以将
文件
作为参数。并将Form1构造函数更改为如下内容

public Form1()
{
    this.InitializeComponent();
    this.InitializeListView();
    var files = Directory.GetFiles("C:\Games\", "*.xml")
                         .Select(f => new ListViewItem(f))
                         .ToArray();
    listView.Items.AddRange(files); 
    foreach(var f in files)
    {
        this.LoadDataFromXml(f);
    }
}

好啊到目前为止你能帮我吗?我不知道是谁干的。你能告诉我它是怎么工作的吗?好的。下一步是添加LoadDataFromXml接收f并用xmlFile替换f所需的内容。我试过了,但没有成功。如果您能帮助我,那就太好了:)@MemorixPilgrim
f
只是一个文件句柄,在调用
LoadDataFromXml
之前,您也需要阅读该文件。Like
XDocument document=XDocument.Load(f)