[英]how to get multiple records with same id if there is multiple <data> element in c# from xml
如果存在多個數據元素,我想要多個記錄,因此對於每個數據記錄,id和log元素將保持相同。
注意:xml中會有多個元素,下面的例子中,下面只是單個元素
xml代碼:
<pay>
<id>1</id>
<data>
<startDate>2016-03-03</startDate>
<adjustedDueDate>2016-03-31</adjustedDueDate>
<Rate>50</Rate>
</data>
<data>
<startDate>2016-04-04</startDate>
<adjustedDueDate>2016-04-04</adjustedDueDate>
<Rate>12</Rate>
</data>
<log>IMP</log>
</pay>
C#代碼
string path = @"E:\XMLFile1.xml";
XDocument xmlExeDoc = XDocument.Load(path);
var recordsFac = xmlExeDoc.Descendants("pay").Select(x => new
{
id = (string)x.Element("id"),
cycle = x.Elements("data").Select(y => new
{
startDate = (y.Elements("startDate").Any() == true) ? (string)y.Element("startDate") : string.Empty,
adjustedDueDate = (DateTime)y.Element("adjustedDueDate"),
Rate = (decimal)y.Element("Rate")
}).Where(y => y.Rate > 0 && y.adjustedDueDate < DateTime.Now)
.Select
(
c => new
{
startDate = c.startDate,
adjustedDueDate = c.adjustedDueDate,
Rate = c.Rate
}
).FirstOrDefault(),
log = x.Element("log")
}).ToList();
所需產量:
輸出:
<pay>
<id>1</id>
<data>
<startDate>2016-03-03</startDate>
<adjustedDueDate>2016-03-31</adjustedDueDate>
<Rate>50</Rate>
</data>
<data>
<startDate>2016-04-04</startDate>
<adjustedDueDate>2016-04-04</adjustedDueDate>
<Rate>12</Rate>
</data>
<log>IMP</log>
</pay>
<ClientData>
<startDate>2016-07-04</startDate>
<adjustedDueDate>2016-08-04</adjustedDueDate>
<Rate>100</Rate>
</ClientData>
<ClientData>
<startDate>2016-09-04</startDate>
<adjustedDueDate>2016-09-04</adjustedDueDate>
<Rate>555</Rate>
</ClientData>
</pay>
var recordsFac = xmlExeDoc.Descendants("pay").Select(x => new
{
cycle = x.Elements("data").Select(y => new
{
id = (string)x.Element("id"),
startDate = (y.Elements("startDate").Any() == true) ? (string)y.Element("startDate") : string.Empty,
adjustedDueDate = (DateTime)y.Element("adjustedDueDate"),
Rate = (decimal)y.Element("Rate"),
log = x.Element("log")
}).Where(y => y.Rate > 0 && y.adjustedDueDate < DateTime.Now),
ClientData = x.Elements("data").Select(y => new
{
id = (string)x.Element("id"),
startDate = (y.Elements("startDate").Any() == true) ? (string)y.Element("startDate") : string.Empty,
adjustedDueDate = (DateTime)y.Element("adjustedDueDate"),
Rate = (decimal)y.Element("Rate"),
log = x.Element("log")
}).Where(y => y.Rate > 0 && y.adjustedDueDate < DateTime.Now)
}).ToList();
上面的代碼是實現這個的正確方法:
輸出:
[1] {1,2016-04-04,2016-04-04,12,IMP}
[3] {1,2016-09-04,2016-09-04,555,IMP}
您的查詢已經產生了您要求的內容,但是您將循環查詢限制為FirstOrDefault,因此它只會產生1條記錄。 如果刪除它,則循環包含兩個記錄。
要獲取id並記錄與日期相同的記錄,只需將它們包含在您正在創建的最終匿名類中:
var recordsFac = xmlExeDoc.Descendants("pay").Select(x => new
{
cycle = x.Elements("data").Select(y => new
{
startDate = (y.Elements("startDate").Any() == true) ? (string)y.Element("startDate") : string.Empty,
adjustedDueDate = (DateTime)y.Element("adjustedDueDate"),
Rate = (decimal)y.Element("Rate"),
}).Where(y => y.Rate > 0 && y.adjustedDueDate < DateTime.Now)
.Select
(
c => new
{
id = (string)x.Element("id"),
startDate = c.startDate,
adjustedDueDate = c.adjustedDueDate,
Rate = c.Rate,
log = x.Element("log")
}
),
}).ToList();
recordsFac將包含xml文件中每個pay元素的記錄。 每條記錄的循環屬性將包含您概述的數據。 一個簡單的嵌套foreach循環將訪問數據:
foreach(var record in recordsFac)
{
foreach(var record2 in record.cycle)
{
Console.WriteLine($"{record2.id},{record2.startDate},{record2.adjustedDueDate},{record2.log}");
}
}
再看看你的代碼,注意到最后一個選擇是多余的,可以刪除:
var recordsFac = xmlExeDoc.Descendants("pay").Select(x => new
{
cycle = x.Elements("data").Select(y => new
{
id = (string)x.Element("id"),
startDate = (y.Elements("startDate").Any() == true) ? (string)y.Element("startDate") : string.Empty,
adjustedDueDate = (DateTime)y.Element("adjustedDueDate"),
Rate = (decimal)y.Element("Rate"),
log = x.Element("log")
}).Where(y => y.Rate > 0 && y.adjustedDueDate < DateTime.Now)
}).ToList();
foreach(var record in recordsFac)
{
foreach(var record2 in record.cycle)
{
Console.WriteLine($"{record2.id},{record2.startDate},{record2.adjustedDueDate},{record2.log}");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.