Xml文件不适用于所有使用C#中的XDocument.Load的事件方法

大恶魔

我想加载一个XML,并使其可用于所有事件。在下面的应用程序中,Button1andButton3事件使用load XML,而Button2不会,我不得不在事件中加载它。我假设每次加载文件时都会占用更多资源,而这是我想避免的。我的问题是:-我是否必须找到其他方法来填充Datagridview-XML如果需要文件加载到其他地方以节省系统资源,是否需要以某种方式卸载文件。

我是编程新手,自学成才,因此如果术语不正确,请提前道歉。

它是Windows形式的应用程序,具有:

  • Button1在ListBox1中生成一个listBox;
  • Button2用2列填充dataGridView1;
  • Button3填充comboBox1列表

XML如下所示:

<?xml version="1.0" encoding="utf-8" ?>
<Config>  
  <Categories>
    <Category Name="OneChar">
      <Entry>
        <Name>a</Name>
        <id>1</id>
      </Entry>
      <Entry>
        <Name>b</Name>
        <id>2</id>
      </Entry>
      <Entry>
        <Name>c</Name>
        <id>3</id>
      </Entry>
    </Category>
    <Category Name="TwoChar">
      <Entry>
        <Name>aa</Name>
        <id>11</id>
      </Entry>
      <Entry>
        <Name>bb</Name>
        <id>22</id>
      </Entry>
      <Entry>
        <Name>cc</Name>
        <id>33</id>
      </Entry>      
    </Category>   
  </Categories>
  <Schemes>
  </Schemes>
</Config>

代码如下:

using System;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml;
using System.Xml.XPath;

namespace List_box_multiple_query
{
    public partial class Form1 : Form
    {
        XDocument xdoc = XDocument.Load("Config\\TestFile.xml");
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            var result = xdoc.XPathSelectElements("/Config/Categories/Category [@Name='TwoChar']/Entry/Name");
            foreach (string entry in result)
            {
                listBox1.Items.Add(entry);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            dataGridView1.Rows.Clear();
            dataGridView1.Refresh();

            XmlDocument doc = new XmlDocument();
            doc.Load("Config\\testfile.xml");
            XmlNodeList nodeList;
            XmlNode root = doc.DocumentElement;
            nodeList = root.SelectNodes("/Config/Categories/Category[@Name='OneChar']/Entry");

            foreach (XmlNode entry in nodeList)
            {               
                int n = dataGridView1.Rows.Add();
                dataGridView1.Rows[n].Cells[0].Value = entry["Name"].InnerText.ToString();
                dataGridView1.Rows[n].Cells[1].Value = entry["id"].InnerText.ToString();                
            }

        }

        private void button3_Click(object sender, EventArgs e)
        {
            var result = xdoc.XPathSelectElements("/Config/Categories/Category [@Name='TwoChar']/Entry/Name");

            foreach (string entry in result)
            {
                comboBox1.Items.Add(entry);
            }
        }
    }
}
用户名

首先,您需要始终使用XDocument或XmlDocument

您可以定义一个

 private Lazy<XmlDocument> docLazy = 
         new Lazy<XmlDocument>(() =>
             { 
                 XmlDocument doc = new XmlDocument();
                 doc.Load("Config\\TestFile.xml");
                 return doc;
             }
         );

然后在所有处理程序中使用它

  var doc = docLazy.Value;

这样,仅在第一次调用时才从文件中加载它,然后将其缓存在内存中。

我先前的答案与之相似,但对于XDocument。

回复评论

有没有一种简单的方法可以选择XML中的节点并使用其内容...?

是的,例如

var test_nodeList  = xdoc.Descendants("Category")
    .Where(x => x.Attribute("Name").Value.Equals("OneChar"))
       .Descendants("Entry");

代替

nodeList = root.SelectNodes("/Config/Categories/Category[@Name='OneChar']/Entry");

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

多个 OnClick 事件不适用于所有对象

方法不适用于数组的所有元素

面板上的C#鼠标事件不适用于面板中的对象

Ruby Lambda不适用于类中的所有方法

ajax调用不适用于c#方法

我试图在我的网站animate.css中实现,它仅适用于jQuery的ready(load)事件,不适用于其他鼠标事件?

“返回”不适用于输入类型文件的更改事件

嵌入的 js 文件不适用于 jQuery .load() 内容

Java DatabaseMetaData.getColumns() 方法不适用于所有用户

VBA/Excel:为什么 match() 方法不适用于所有“类型”的范围

onKeyDown事件不适用于React中的div

清除间隔不适用于javascript中的transitionend事件

触摸事件不适用于Angular中的Google Maps

更改事件不适用于角度4中的跨度

Jquery 事件不适用于 append() 中的元素

Javascript中的反跳不适用于Wheel事件

指针事件:无;不适用于Edge中的iframe

Jquery 函数不适用于标头。当我使用 .load() 方法包含标题时

C#:工作簿的关闭方法不适用于在 OneDrive 上打开的工作簿

同步是否适用于类中的所有方法或仅适用于 1 个方法?

QuerySelectorAll不适用于onclick事件

onClick事件不适用于Android

jQuery不适用于按钮事件

onMarkerClick事件不适用于ClusterManager

javascript事件不适用于循环

onmouseup事件不适用于滑块

jQuery事件不适用于id

clearTimeout 不适用于 mouseup 事件

文件类型中的方法delete(File)不适用于参数(路径