I have an combobox that filled with xml data. The valu member is from xml file. I would like read the selected element from xml file.
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 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. After this I would like to read the elemnts from xml file where ToolID is the combobox selected 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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.