简体   繁体   English

使用linq基于xml从xml获取xml列表,如果不存在,则在c#中选择false条件标记元素

[英]get list from xml using linq based on true if there is no true then select false condition tag element in c#

here i am trying to get custName name if pirmaryCustCheck is true if not then i have to select false tag element , how can i achieve this xml : 在这里,如果pirmaryCustCheck为true,那么我尝试获取custName名称;否则,我必须选择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>

code : 代码:

   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();

You may want to try something like below. 您可能想尝试以下类似方法。

Data Set 数据集

<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>

Code - 代码-

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();

Try below code, its tested. 请尝试以下代码,对其进行测试。

var customers = (from e in docss.Descendants("data").Elements("customer")
                         where (bool)e.Element("pirmaryCustCheck") == true
                         select new 
                         {
                             Name = (string)e.Element("custName"),
                             Id = (string)e.Element("id"),
                         }).ToList();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 LINQ 根据条件用 true 或 false 填充布尔值 - Using LINQ to fill a boolean with true or false based off of condition 使用 Select() 从条件为真的列表中获取值 - Get a value from a list where a condition is true using Select() 查找清单的值 <object> 使用C#和linq导致产生真实条件 - Find value of list<object> that cause to make true condition using C# and linq 如何更改属性类型 <TRUE/FALSE> 在XML中使用C# - How to change attribute type <TRUE/FALSE> in xml using c# C# 解析“(真和真)或(真或假)” - C# resolve “(true and true) or (true or false)” c#List.Exists返回true并带有“ false &amp;&amp; true” - c# List.Exists returns true with “false && true” 根据条件LINQ TO XML C#选择元素 - Select elements based on condition LINQ TO XML C# C#如果字符串与使用LINQ的字符串列表中的任何字符串不同,则返回true - C# Return true if string is different from any of the string in a list of strings using LINQ 哪个 C# XML 文档注释标记用于“true”、“false”和“null”? - Which C# XML documentation comment tag is used for 'true', 'false' and 'null'? C#LINQ Orderby - true / false如何影响orderby? - C# LINQ Orderby - How does true/false affect orderby?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM