简体   繁体   中英

How do I load multiple XML files and read them?

I've a problem. In my code I'm looking for all xml files in one directory. Finally it works but I don't know how to read them. A single one is loaded with the string xmlFile. I think I need to replace or edit that in this way that my code now that xmlFile is not only one file, but all files who are found in the directory.

What should I be doing?

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; }
    }
}

UPDATE:

This is my current code. how can i send f to 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);
                }


    }

Simple use Directory functions to get all your XML files, and loop through them, by calling LoadDataFromXml for each file. Note: You will need to refactor your code a little bit.

You need to modify your LoadDataFromXml to take file as a parameter. And change your Form1 constructor to something like this

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM