读取xml文件并将id值写入c#中的相关文本框中

时间:2022-12-17 15:07:47

I have a xml file named "numbers.xml" like this:

我有一个名为“数字”的xml文件。xml”是这样的:

<?xml version="1.0" encoding="utf-8" ?>
<program>
<box id="aaa" value="78678"/>
<box id="bbb" value="37287"/>
<box id="ccc" value="783"/>
<box id="ddd" value="7867"/>
<box id="eee" value="786"/>
<box id="fff" value="23"/>
<box id="ggg" value="453"/>
<box id="hhh" value="4537"/>
</program>

I want to read this xml file and fill textboxes. But in windows forms application txtAAA.text value must take id="aaa" value which is 78678. Likewise txtBBB.text value must take id="bbb" value which is 37287. How can I do this?

我想读取这个xml文件并填充文本框。但是在windows窗体应用程序txtAAA中。文本值必须取id="aaa"值,即78678。同样txtBBB。文本值必须取id=“bbb”值,即37287。我该怎么做呢?

Edit:

编辑:

I tried like this:

我试着像这样:

 XmlDocument xmldoc = new XmlDocument();
 xmldoc.Load(openfiledialog1.FileName); 
 XmlNodeList nodelist = xmldoc.DocumentElement.ChildNodes;
 XmlNode xmlnode = nodelist.Item(0); 
 txtAAA.Text = xmlnode.Attributes["id"].InnerText; 

But "aaa" is shown in textbox. It was totally failure. –

但是“aaa”显示在文本框中。这是完全的失败。- - - - - -

3 个解决方案

#1


2  

You could create a list of the xml items and assign the based on the TextBox name in a foreach loop

您可以创建xml项的列表,并根据foreach循环中的文本框名称分配它们

Assuming TextBox names are:

假设文本框的名称是:

txtAAA
txtBBB
txtCCC
...etc

you can just remove the txt part to find the correct value id

您可以删除txt部分以找到正确的值id

var data = XElement.Load("C:\\Test.xml").Descendants("box");

foreach (var textbox in Controls.OfType<TextBox>())
{
    var value = data.FirstOrDefault(v => v.Attribute("id").Value == textbox.Name.Replace("txt","").ToLower());
    if (value != null)
    {
        textbox.Text = value.Attribute("value").Value;
    }
}

Test:

测试:

读取xml文件并将id值写入c#中的相关文本框中

#2


3  

nMaybe this line of code will help you:

也许这行代码会帮助你:

TextBox textBox = this.Controls.OfType<TextBox>().FirstOrDefault(tb => tb.Name == "idFromXMl");

In your case, where you have more textboxes I would save the result of this.Controls.OfType<TextBox>() in a collection and the work on with this.

在您的例子中,如果有更多的文本框,我将把this. controls . oftype ()的结果保存到一个集合中,并对其进行处理。

#3


2  

You can use XPath to find the node.

可以使用XPath查找节点。

Then find the attribute, 'value'.

然后找到属性“value”。

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load( openfiledialog1.FileName );
        XmlNode nodeAAA = xmldoc.SelectSingleNode( "/program/box[@id='aaa']" ); //XPath Query here.
        txtAAA.Text = nodeAAA.Attributes["value"].InnerText;

XPath Tutorial - http://www.w3schools.com/xpath/xpath_syntax.asp

XPath教程——http://www.w3schools.com/xpath/xpath_syntax.asp

#1


2  

You could create a list of the xml items and assign the based on the TextBox name in a foreach loop

您可以创建xml项的列表,并根据foreach循环中的文本框名称分配它们

Assuming TextBox names are:

假设文本框的名称是:

txtAAA
txtBBB
txtCCC
...etc

you can just remove the txt part to find the correct value id

您可以删除txt部分以找到正确的值id

var data = XElement.Load("C:\\Test.xml").Descendants("box");

foreach (var textbox in Controls.OfType<TextBox>())
{
    var value = data.FirstOrDefault(v => v.Attribute("id").Value == textbox.Name.Replace("txt","").ToLower());
    if (value != null)
    {
        textbox.Text = value.Attribute("value").Value;
    }
}

Test:

测试:

读取xml文件并将id值写入c#中的相关文本框中

#2


3  

nMaybe this line of code will help you:

也许这行代码会帮助你:

TextBox textBox = this.Controls.OfType<TextBox>().FirstOrDefault(tb => tb.Name == "idFromXMl");

In your case, where you have more textboxes I would save the result of this.Controls.OfType<TextBox>() in a collection and the work on with this.

在您的例子中,如果有更多的文本框,我将把this. controls . oftype ()的结果保存到一个集合中,并对其进行处理。

#3


2  

You can use XPath to find the node.

可以使用XPath查找节点。

Then find the attribute, 'value'.

然后找到属性“value”。

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load( openfiledialog1.FileName );
        XmlNode nodeAAA = xmldoc.SelectSingleNode( "/program/box[@id='aaa']" ); //XPath Query here.
        txtAAA.Text = nodeAAA.Attributes["value"].InnerText;

XPath Tutorial - http://www.w3schools.com/xpath/xpath_syntax.asp

XPath教程——http://www.w3schools.com/xpath/xpath_syntax.asp