使用 XML 元素 [英] Using XML elements

查看:25
本文介绍了使用 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有工作代码,我只想在程序的其他区域多次引用部分代码,但是我的问题似乎与匿名类型(var)有关

I have working code that I'd simply like to reference parts of multiple times in other area's of my program, however my problem seems to be with anonymous types (var)

这是我的程序的一部分,用于加载我的 XML 文件

here's a section of my program that loads my XML file

string path = "Data//handling4.meta";
            var doc = XDocument.Load(path);
            var items = doc.Descendants("HandlingData").Elements("Item");
            var query = from x in items

                        select new
                        {
                            HandlingName = (string)x.Element("handlingName"),
                            HandlingType = (string)x.Element("HandlingType"),
                            Mass = (decimal?)x.Element("fMass").Attribute("value"),
                            InitialDragCoeff = (decimal?)x.Element("fInitialDragCoeff").Attribute("value"),
                            PercentSubmerged = (decimal?)x.Element("fPercentSubmerged").Attribute("value"),
                            DriveBiasFront = (decimal?)x.Element("fDriveBiasFront").Attribute("value")
                        };

这段代码效果很好,我也可以使用这样的 foreach 循环来引用项目

This code works great, I can also use a foreach loop like this to reference items

 foreach(var HandlingName in query)
        {
            string Names = HandlingName.HandlingName;
        }           

所以我的问题是,如何将其引用到程序中的另一个位置?例如

private void button1_Click(object sender, EventArgs e)
        {
            comboBox1.Items.Add( Names);
        }

上面的代码抛出错误'Names does not exist in the current context'

The above code throws the error 'Names does not exist in the current context'

我需要让我的组合框显示每个处理名称,但它失败了,因为无法引用匿名类型.我做错了什么,还是我没有使用不正确的方法?谢谢

I need for my comboBox to display each handling name, but it fails because anonymous types cannot be referenced. Am I doing something wrong, or am I just not using the incorrect method? Thanks

还要提到需要在程序的多个部分引用 XML,例如,单击按钮甚至会显示 DataGridView 中的值.如果我可以创建一个类来加载 XML,那么程序的不同区域中的每个元素都将是理想的,而不是每次加载庞大的代码块

Also to mention that the XML is needing to be referenced in multiple parts of the program, button click even shows the values in a DataGridView for example. If I could make a class to load the XML, then each element in different area's of the program that'd be ideal, rather than loading the hefty block of code each time

推荐答案

需要在程序的多个部分引用 XML,

the XML is needing to be referenced in multiple parts of the program,

但是你所有的变量都是本地的.您需要在 Form fiad 或 property 中存储一些东西.

But all your variables are local. You need to store something in a Form fiead or property.

// outside any method:
private List<string> names = new List<string>();

void myLoadMethod()
{
    ...
    foreach(var HandlingName in query)
    {
        //string Names = HandlingName.HandlingName;
        Names.Add(HandlingName.HandlingName);
    }  
}

private void button1_Click(object sender, EventArgs e)
{
    comboBox1.Items.Add( Names);
}

这篇关于使用 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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