Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 选择xml中的特定子节点?_C#_.net_Xml_Winforms_Linq - Fatal编程技术网

C# 选择xml中的特定子节点?

C# 选择xml中的特定子节点?,c#,.net,xml,winforms,linq,C#,.net,Xml,Winforms,Linq,我有一些看起来像这样的XML <SolutionString> <Solutions> <Solution> <ID>1</ID> <Property> <Name>DriverSheave</Name> <Value>1VP34</Value> </Property>

我有一些看起来像这样的XML

    <SolutionString>
  <Solutions>
    <Solution>
      <ID>1</ID>
      <Property>
        <Name>DriverSheave</Name>
        <Value>1VP34</Value>
      </Property>
      <Property>
        <Name>DriverBushing</Name>
        <Value>
        </Value>
      </Property>
      <Property>
        <Name>DrivenSheave</Name>
        <Value>AK49</Value>
      </Property>
      <Property>
        <Name>DrivenBushing</Name>
        <Value>
        </Value>
      </Property>
      <Property>
        <Name>Belt</Name>
        <Value>AX30</Value>
      </Property>
      <Property>
        <Name>BeltQty</Name>
        <Value>1</Value>
      </Property>
      <Property>
        <Name>ActualCenterDistance</Name>
        <Value>9.88</Value>
      </Property>
     <Property>
        <Name>ActualServiceFactor</Name>
        <Value>1.71</Value>
      </Property>
     <Property>
        <Name>ActualDrivenShaftSpeed</Name>
        <Value>745/1117</Value>
      </Property>
     <Property>
        <Name>Cost</Name>
        <Value>32.65</Value>
      </Property>
      <TechSpecs>
        <TurnsOpen>2.5/4.0</TurnsOpen>
        <HubLoad>55 lb. 'running' Hub Load</HubLoad>
        <Tension>1.93 lb. should deflect belt 0.15 in.</Tension>
      </TechSpecs>
      <Property>
        <Name>Comment1</Name>
        <Value>If Driver is 2.5 turns open Driven RPM=931 and CD=10.25 in. If Driver is 4.0 turns open Driven RPM=819 and CD=10.47 in.</Value>
      </Property>
      <Property>
        <Name>Comment2</Name>
        <Value>Correct tension for this drive (1.93 lb. should deflect belt 0.15 in.) will have 55 lb. 'running' Hub Load</Value>
      </Property>
      <Interchanges>
        <Interchange>
          <DriverSheave />
          <DriverBushing />
          <DrivenSheave>BK55X7/8</DrivenSheave>
          <DrivenBushing>
          </DrivenBushing>
        </Interchange>
        <Interchange>
          <DriverSheave />
          <DriverBushing />
          <DrivenSheave>AK49H</DrivenSheave>
          <DrivenBushing>H</DrivenBushing>
        </Interchange>
        <Interchange>
          <DriverSheave />
          <DriverBushing />
          <DrivenSheave>BK55H</DrivenSheave>
          <DrivenBushing>H</DrivenBushing>
        </Interchange>
      </Interchanges>
    </Solution>
    <Solution>
我遇到的问题是,对于每个
解决方案
/ID,都有12个子
属性
。我希望能够指定其中的6个,例如选择第一个

<Property>
    <Name>DriverSheave</Name>
    <Value>1VP34</Value>
</Property>

驾驶员举
1VP34
第三个,第六个,跳过其他的


我看到了这一点,但我不确定如何在我的情况下实施这一点

这个答案将帮助您:


您可以这样做:

var indexesToChoose = new List<int> {1, 2};

var cat = solutions
            .Descendants("Solution")
            .Select(x => new
            {
                ID = (string)x.Element("ID"),
                Properties = x.Elements("Property")
                    .Select((p, i) => new
                    {
                        Name = (string)p.Element("Name"),
                        Value = (string)p.Element("Value"),
                        idx = i
                    })
                    .Where(y => indexesToChoose.Contains(y.idx))
                    //.OrderBy(z => indexesToChoose.FindIndex(p => p == z.idx))
                    .ToList()
            });
var indexetostochoose=新列表{1,2};
var cat=解决方案
.后代(“解决方案”)
.选择(x=>new
{
ID=(字符串)x.Element(“ID”),
属性=x.Elements(“属性”)
.选择((p,i)=>新建
{
名称=(字符串)p.Element(“名称”),
Value=(字符串)p.Element(“Value”),
idx=i
})
.Where(y=>indexstochoose.Contains(y.idx))
//.OrderBy(z=>indextochoose.FindIndex(p=>p==z.idx))
托利斯先生()
});
在这里,我们首先创建要选择的索引列表,然后在创建每个匿名类实例的过程中对它们进行枚举,最后只取那些在
索引中有索引的索引进行选择


结果-在执行上述代码后,属性字段将只包含xml中的第一个和第二个元素。

这看起来像我想要的,但我的Linq知识不稳定,我在哪里实现这段代码?我的代码是这样的,
var nthitem=cat.skip(1).first()?是的,这应该可以做到,一旦你选择了解决方案,然后你想找到它的第n个孩子。我建议将
cat
变量的名称更改为
solution
这看起来很完美,因此如果我想要2,7,10,我只需执行
var indexetostochoose=new List,{2,7,10}
我看到的Linq越多,SQL语句看起来就越像,我只是想学一下格式,哈哈。@AndrewKucenski是的,没错。如果我把我想要的东西像这样放进去,
{7,2,10}
会按顺序排列吗,还是会是一个单独的
.orderby
?@AndrewKucenski肯定不会按顺序排列,它只是过滤。如果您也需要使用
indexetostochoose
作为排序序列,那么您可以在
之前使用
.OrderBy(z=>indexetostochoose.FindIndex(p=>p==z.idx))
这样做非常有效,但是,我在示例中删除了我的XML,但是这个XML有ID2,然后是相同的属性列表,然后是id3,所以你的代码得到了ID1的正确属性,但是ID2-10是空白的,因为我假设ID2中的属性是{13,16,18}yadda yadda,所以我的代码不会查看/获取未来的属性,我假设我需要在idx=i++上进行某种形式的重置,每次ID迭代?
 var cat = solutions
    .Descendants("Solution")
    .Select(x => new
    {
        ID = (string)x.Element("ID"),
        Properties = x.Elements("Property").Select(p => new
        {
            Name = (string) p.Element("Name"),
            Value = (string) p.Element("Value")
        }).ToList()
    });

var items = cat
    .Select(s => new
    {
        ID = s.ID,
        Text = string.Format("{0}. {1}", s.ID,
        string.Join(", ", s.Properties
                           .Select(p => string.Format("{0} = {1}",
                               p.Name,
                               p.Value ?? "(null)"))))
    }).ToArray();
comboBox1.DisplayMember = "Text";
comboBox1.ValueMember = "ID";
comboBox1.Items.AddRange(items);
<Property>
    <Name>DriverSheave</Name>
    <Value>1VP34</Value>
</Property>
var nthItem = items.Skip(n).First();
var indexesToChoose = new List<int> {1, 2};

var cat = solutions
            .Descendants("Solution")
            .Select(x => new
            {
                ID = (string)x.Element("ID"),
                Properties = x.Elements("Property")
                    .Select((p, i) => new
                    {
                        Name = (string)p.Element("Name"),
                        Value = (string)p.Element("Value"),
                        idx = i
                    })
                    .Where(y => indexesToChoose.Contains(y.idx))
                    //.OrderBy(z => indexesToChoose.FindIndex(p => p == z.idx))
                    .ToList()
            });