首页 > 解决方案 > 使用 XML 文件中的组合数据填充组合框

问题描述

我有一个这样的 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>

我想将数据填充到组合框:

displaymember= <TooDia>"x"<ToolApmxs>" mm - "<ToolName>;
valuemember=<ToolID>;

我试过这段代码:

var xmlDocument = XDocument.Load(@"data\tools.xml");
var toolist = xmlDocument.Descendants("ToolClass");

但是如何将数据组合成指定的格式呢?

谢谢!

标签: c#xmllinqcombobox

解决方案


所以你可以使用这样的东西来获取第一个成员的内部文本:

var xmlDocument = XDocument.Load(@"data\tools.xml");

var tooList = xmlDocument.GetElementsByTagName("ToolClass").Item(0).InnerText;

推荐阅读