[英]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.