[英]Get selected element by combobox from XML file
I have an combobox that filled with xml data. 我有一个装满xml数据的组合框。 The valu member is from xml file.
值成员来自xml文件。 I would like read the selected element from xml file.
我想从xml文件中读取选定的元素。
var xmlDocument = XDocument.Load(@"data\tools.xml");
var ToolData = from r in xmlDocument.Descendants("ToolClass").Where
(r => (string)r.Attribute("ToolID") == ToolListComboBox.SelectedValue.ToString())
select new
{
Tooldia = r.Element("ToolDia").Value,
Tooltooth = r.Element("ToolTooth").Value,
Toolfeed= r.Element("ToolFeedPerTooth").Value,
Toolcut = r.Element("ToolCuttingSpeed").Value
};
foreach(var r in ToolData)
{
CalcToolDia.Text = r.Tooldia.ToString();
}
I tried with this code, but not work. 我尝试使用此代码,但不起作用。
EDIT: I have this XMl file: 编辑:我有此XMl文件:
<?xml version="1.0"?>
<ArrayOfToolClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ToolClass>
<ToolID>1</ToolID>
<ToolName>Multiflute Endmill</ToolName>
<ToolDia>8</ToolDia>
<ToolTooth>4</ToolTooth>
<ToolApmxs>32</ToolApmxs>
<ToolCuttingSpeed>150</ToolCuttingSpeed>
<ToolFeedPerTooth>0.04</ToolFeedPerTooth>
<ToolAe>8</ToolAe>
<ToolAp>4</ToolAp>
<ToolManufacturer>SECO</ToolManufacturer>
<ToolSerial>DKFLJDSKJ</ToolSerial>
</ToolClass>
<ToolClass>
<ToolID>2</ToolID>
<ToolName>Multiflute Endmill</ToolName>
<ToolDia>4</ToolDia>
<ToolTooth>4</ToolTooth>
<ToolApmxs>25</ToolApmxs>
<ToolCuttingSpeed>235</ToolCuttingSpeed>
<ToolFeedPerTooth>0.03</ToolFeedPerTooth>
<ToolAe>4</ToolAe>
<ToolAp>0.4</ToolAp>
<ToolManufacturer>SECO</ToolManufacturer>
<ToolSerial>DJFKLSL</ToolSerial>
</ToolClass>
</ArrayOfToolClass>
The combobox contain this: 组合框包含以下内容:
var xmlDocument = XDocument.Load(@"data\tools.xml");
var toolist = xmlDocument.Descendants("ToolClass")
.Select(tc => new
{
Display = tc.Element("ToolDia").Value + "x" + tc.Element("ToolApmxs").Value + " mm - " + tc.Element("ToolName").Value,
Value = tc.Element("ToolID").Value
}).ToList();
ToolListComboBox.DisplayMember = "Display";
ToolListComboBox.ValueMember = "Value";
ToolListComboBox.DataSource = toolist;
When I select an item in combobox then she give me the ToolID. 当我在组合框中选择一个项目时,她会给我ToolID。 After this I would like to read the elemnts from xml file where ToolID is the combobox selected ToolID.
之后,我想从xml文件中读取元素,其中ToolID是组合框选择的ToolID。
If I am understand correctly you have an elements value and you want to retrieve the entire element providing this value - maybe try below. 如果我正确理解您有一个元素值,并且想要检索提供此值的整个元素,请尝试以下操作。
To modify my sample provided to align to your example:
XElement root = XElement.Load(@"data\tools.xml");
IEnumerable<XElement> address =
from el in root.Elements("ToolClass")
where (string)el.Attribute("ToolID") ==ToolListComboBox.SelectedValue.ToString()
select el;
foreach (XElement el in address)
Console.WriteLine(el.Tooldia.ToString());
I think mistake is - where you are using attribute instead of element. 我认为错误是-您使用属性而不是元素。 Change it to
更改为
(string)r.Element("ToolID").Value
This is how it will look 这就是它的样子
var xmlDocument = XDocument.Load(@"data\tools.xml");
var ToolData = from r in xmlDocument.Descendants("ToolClass").Where
(r => (string)r.Element("ToolID").Value == ToolListComboBox.SelectedValue.ToString())
select new
{
Tooldia = r.Element("ToolDia").Value,
Tooltooth = r.Element("ToolTooth").Value,
Toolfeed= r.Element("ToolFeedPerTooth").Value,
Toolcut = r.Element("ToolCuttingSpeed").Value
};
foreach(var r in ToolData)
{
CalcToolDia.Text = r.Tooldia.ToString();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.