如何加载多个XML文件并阅读它们? [英] How do I load multiple XML files and read them?

查看:115
本文介绍了如何加载多个XML文件并阅读它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。在我的代码中,我正在寻找一个目录中的所有xml文件。最后它工作,但我不知道如何阅读它们。单个字符串加载了字符串xmlFile。我想我需要以这种方式替换或编辑,我的代码现在xmlFile不仅是一个文件,而且是目录中找到的所有文件。



什么我应该在做什么?

 命名空间WindowsFormsApplication11 
{
public partial class Form1:Form
{
private const string xmlFile =C:\Games\games.xml; //单个xml文件工程

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。负载(XMLFILE);
if(document.Root!= null)
{
foreach(XElement gameElement in document.Root.Elements(game))
{
string gamename = 。gameElement.Element( gamename)值;
string launchpath = gameElement.Element(launchpath)。
string uninstallpath = gameElement.Element(uninstallpath)。
string publisher = gameElement.Element(publisher)。
//检查gameElement.Element(ELEMENTNAME)是否不为空
游戏游戏=新游戏(gamename,launchpath,uninstallpath,publisher);
AddGameToListView(游戏);
}
}
}
}

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

私人ListViewItem CreateGameListViewItem(游戏游戏)
{
ListViewItem item = new ListViewItem(game.Gamename);
item.SubItems.Add(game.Launchpath);
item.SubItems.Add(game.Uninstallpath);
item.SubItems.Add(game.Publisher);
item.Tag = game;
返回项目;
}



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)
{
游戏游戏=(游戏)((this.listView.SelectedItems [0] .Tag);
尝试
{
Process.Start(game.Launchpath);
}
catch(Exception ex)
{
MessageBox.Show(Can not start game.\DDails:\\\
+ ex.Message,Error,MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}
}
}

内部类Game
{
public Game()
{

}

public Game(string gamename,string launchpath,string uninstallpath,字符串发布者)
{
this.Gamename = gamename;
this.Launchpath = launchpath;
this.Uninstallpath = uninstallpath;
this.Publisher = publisher;
}
public string Gamename {get;组; }
public string Launchpath {get;组; }
public string Uninstallpath {get;组; }
public string Publisher {get;组;
}
}

更新:



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

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

}

private void LoadDataFromXml(//我需要输入这里?)
{


foreach(XElement gameElement in f.Root.Elements(game))
{
string gamename = gameElement.Element(gamename)。
string launchpath = gameElement.Element(launchpath)。
string portablesave = gameElement.Element(portablesave)。
string publisher = gameElement.Element(publisher)。
string gameid = gameElement.Element(gameID)。
string update = gameElement.Element(update)。
//检查gameElement.Element(ELEMENTNAME)是否不为空
游戏=新游戏(gamename,launchpath,portablesave,publisher,gameid,update);
AddGameToListView(游戏);
}


}


解决方案

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



您需要修改 LoadDataFromXml code> file 作为参数。并将您的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);
}
}


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

这篇关于如何加载多个XML文件并阅读它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆