如果没有,则使用linq基于true从xml获取列表然后在c#中选择false condition标记元素

时间:2016-10-05 06:03:04

标签: c# xml linq

这里我试图获取custName名称,如果pirmaryCustCheck为真,如果没有那么我必须选择false标签元素,我怎样才能实现这一点 xml:

<Root>
  <data>
    <office>Mumba</office>
    <officeId>1JC9FJBM</officeId>
    <customer>
        <custName>Yash</custName>
        <pirmaryCustCheck>true</pirmaryCustCheck>
        <id>8</id>
      </customer>
    <customer>
      <custName> Rahul</custName>
        <pirmaryCustCheck>false</pirmaryCustCheck>
        <id>9</id>
      </customer>
  </data>
</Root>

代码:

   string pathd = @"C:\Users\admin\documents\newCust.xml";

            XDocument docss = XDocument.Load(pathd);


            var records = docss.Descendants("data").Select(x => new
            {

                office = (string)x.Element("office"),
                officeId = (string)x.Element("officeId"),
                customer = x.Elements("customer").Select(y => new
                {
                    custName = (string)y.Element("custName"),
                    pirmaryCustCheck = (bool)y.Element("pirmaryCustCheck"),
                    id = (string)y.Element("id")
                }).Where(y => y.pirmaryCustCheck == true).Select(c => new
                {
                    custName = c.custName,
                    Id = c.id
                })
            }).FirstOrDefault();

2 个答案:

答案 0 :(得分:0)

您可能想尝试下面的内容。

数据集

<Root>
<data>
<office>Mumba</office>
<officeId>1JC9FJBM</officeId>
<customer>
    <custName>Yash</custName>
    <pirmaryCustCheck>true</pirmaryCustCheck>
    <id>8</id>
  </customer>
<customer>
  <custName> Rahul</custName>
    <pirmaryCustCheck>false</pirmaryCustCheck>
    <id>9</id>
  </customer>
 </data>
 <data>
<office>Mumba</office>
<officeId>1JC9FJBM</officeId>
<customer>
    <custName>Yash</custName>
    <pirmaryCustCheck>false</pirmaryCustCheck>
    <id>11</id>
  </customer>
<customer>
  <custName> Rahul Jain</custName>
    <pirmaryCustCheck>false</pirmaryCustCheck>
    <id>10</id>
  </customer>
</data>

代码 -

string pathd = @"C:\Users\admin\documents\newCust.xml";

        XDocument docss = XDocument.Load(pathd);

        var customerTrue = docss.Descendants("data").Elements("customer").Select(x => x).FirstOrDefault();
        var customerFalse = docss.Descendants("data").Elements("customer").Select(x => x).LastOrDefault();

        var records = docss.Descendants("data").Select(x => new
        {

            office = (string)x.Element("office"),
            officeId = (string)x.Element("officeId"),
            customer = new
            {
                custName = (bool)x.Elements("customer").First().Element("pirmaryCustCheck") ? (string)x.Elements("customer").First().Element("custName") : (string)x.Elements("customer").Last().Element("custName"),
                pirmaryCustCheck = (bool)x.Elements("customer").First().Element("pirmaryCustCheck"),
                id = (bool)x.Elements("customer").First().Element("pirmaryCustCheck") ? (string)x.Elements("customer").First().Element("id") : (string)x.Elements("customer").Last().Element("id")
            }
        }).ToList();

答案 1 :(得分:0)

尝试以下代码,经过测试。

import java.util.*;
class removeDuplicate{
int [] y ;

public removeDuplicate(int[] array){
    y=array;

    for(int b=0;b<y.length;b++){
        int temp = y[b];
        for(int v=0;v<y.length;v++){
            if( b!=v && temp==y[v]){
                y[v]=0;
            }
        }
    }
}