[英]Filter XML collection based on child node data in c#
我有以下學生的 XML 集合,如下所示。
<stdXmlFormat version="1.0">
<students>
<field id="ClassId">CB0012</field>
<field id="ClassName">CBSE-12</field>
<student id="123">
<field id="SFirstNm">Ram</field>
<field id="SLastNm">Raju</field>
<field id="RollNumber">001</field>
</student>
<student id="124">
<field id="SFirstNm">Sita</field>
<field id="SLastNm">M</field>
<field id="RollNumber">002</field>
</student>
<collection id="StudentCollection">
<record>
<field id="StudentId">5094e0ef-c966-484d-9892-e62bd828e7cf</field>
<field id="FirstName">Ram</field>
<field id="LastName">Raju</field>
</record>
<record>
<field id="StudentId">70a0350a-9556-46f6-b089-bebcc278b1c1</field>
<field id="FirstName">Sita</field>
<field id="LastName">M</field>
</record>
</collection>
<collection id="AccountTransactions">
<record>
<field id="Id">62f4181a-5510-4522-a24a-7d3005f2a907</field>
<field id="TransactionType">Fees</field>
<field id="Balance">5000.00</field>
</record>
<record>
<field id="Id">0958d991-c777-46b3-954c-3682ff735bfc</field>
<field id="TransactionType">Fine</field>
<field id="Balance">2000.00</field>
</record>
<record>
<field id="Id">3aadb37d-d066-491f-8525-2e299ad8d88b</field>
<field id="TransactionType">Fees</field>
<field id="Balance">6000.00</field>
</record>
</collection>
<collection id="StudentAccountTransactions">
<record>
<field id="Id">5e7d0c97-8759-4beb-a688-009e23f10590</field>
<field id="StudentId">5094e0ef-c966-484d-9892-e62bd828e7cf</field>
<field id="AccountTransactionsId">62f4181a-5510-4522-a24a-7d3005f2a907</field>
</record>
<record>
<field id="Id">b22bf993-4dc2-49e6-879f-504ca4ec8424</field>
<field id="StudentId">5094e0ef-c966-484d-9892-e62bd828e7cf</field>
<field id="AccountTransactionsId">0958d991-c777-46b3-954c-3682ff735bfc</field>
</record>
<record>
<field id="Id">44641d91-38bf-4e24-895c-20f92f390acf</field>
<field id="StudentId">70a0350a-9556-46f6-b089-bebcc278b1c1</field>
<field id="AccountTransactionsId">3aadb37d-d066-491f-8525-2e299ad8d88b</field>
</record>
</collection>
</students>
</stdXmlFormat>
學生節點是父節點,它由子節點和集合( StudentCollection 、 AccountTransactions和StudentAccountTransactions )組成。
<stdXmlFormat version="1.0">
<students>
<student></student>
<collection id="StudentCollection"/>
<collection id="AccountTransactions"/>
<collection id="StudentAccountTransactions"/>
</students>
</stdXmlFormat>
學生節點包含所有學生詳細信息,例如名字、姓氏、卷號等(這是一個包含更多信息的舊集合。為了更好地閱讀和理解,我刪除了一些屬性)
新集合StudentCollection中提供了相同的信息,但數據較少。此集合具有StudentId (用於識別其他集合中的學生的唯一 guid)。 與學生和StudentCollection的唯一關系是學生的名字和姓氏。
AccountTransactions是包含 id(用於識別每筆交易的唯一指南)和其他交易詳細信息的集合。 此集合中的 id 用作StudentAccountTransactions集合中的 AccountTransactionsId。
最終的集合StudentAccountTransactions包含 studentid(來自 StudentCollection)和 AccountTransactionsId(來自 AccountTransactions 集合)。
所以從這個列表中我需要獲取學生的詳細信息並將其傳遞給 DB TEAM 以保存到數據庫。他們准備好了表結構,如下所示
基本上我需要從這個集合中獲取 SFirstNm、SLastNm、RollNumber(來自學生)、StudentId、AccountTransactionsId 和 StudentAccountTransactionsId。
我嘗試使用 XMLdocument 和 Xdocument 獲取數據,但沒有任何效果
string xpath = @"/stdXmlFormat/students/students/field";
XmlNodeList nodeListstudent = root.SelectNodes(xpath);
foreach (XmlNode item in nodeListstudent )
{
string SID = item.Attributes["id"].Value;
string sFirstNm, sLastNm = string.Empty;
foreach (XmlNode childNodes in item .ChildNodes)
{
string NodeName = childNodes.Attributes["id"].Value;
string NodeValue = childNodes.InnerText;
if (NodeName == "sFirstNm")
{
sFirstNm= NodeValue;
}
if (NodeName == "aBLastNm")
{
sLastNm = NodeValue;
}
}
}
由此我能夠獲得學生的名字和姓氏等,但是我如何使用名字和姓氏從StudentCollection獲取學生 ID? 我真的被困在這里請幫忙。 使用該 studentid,我需要獲取其他詳細信息,例如 AccountTransactionsId 和 StudentAccountTransactionsId。
我嘗試使用以下方式過濾學生身份集合以進行測試,但沒有奏效。
XmlNodeList nodes = jsonXmlDocument.SelectNodes("//stdXmlFormat/students/collection/record/field[@id='5094e0ef-c966-484d-9892-e62bd828e7cf']");
請幫我 !
這是一個開始:
using System;
using System.Linq;
using System.Text;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApp2
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("StudentId", typeof(string));
dt.Columns.Add("SFirstNm", typeof(string));
dt.Columns.Add("SLastNm", typeof(string));
dt.Columns.Add("RollNumber", typeof(string));
dt.Columns.Add("AccountTransaction", typeof(string));
dt.Columns.Add("StudentAccountTransaction", typeof(string));
XDocument doc = XDocument.Load(FILENAME);
XElement xStudents = doc.Descendants("students").FirstOrDefault();
var students = xStudents.Elements("student")
.Select(x => new
{
id = (string)x.Attribute("id"),
sFName = (string)x.Elements("field").Where(y => (string)y.Attribute("id") == "SFirstNm").FirstOrDefault(),
sLName = (string)x.Elements("field").Where(y => (string)y.Attribute("id") == "SLastNm").FirstOrDefault(),
rollNumber = (string)x.Elements("field").Where(y => (string)y.Attribute("id") == "RollNumber").FirstOrDefault(),
}).ToList();
XElement xStudentCollection = doc.Descendants("collection").Where(x => (string)x.Attribute("id") == "StudentCollection").FirstOrDefault();
var studentCollection = xStudentCollection.Elements("record")
.Select(x => new
{
sId = (string)x.Elements("field").Where(y => (string)y.Attribute("id") == "StudentId").FirstOrDefault(),
sFName = (string)x.Elements("field").Where(y => (string)y.Attribute("id") == "FirstName").FirstOrDefault(),
sLName = (string)x.Elements("field").Where(y => (string)y.Attribute("id") == "LastName").FirstOrDefault(),
}).ToList();
XElement xAccountTransactions = doc.Descendants("collection").Where(x => (string)x.Attribute("id") == "AccountTransactions").FirstOrDefault();
var accountTransactions = xAccountTransactions.Elements("record")
.Select(x => new
{
tId = (string)x.Elements("field").Where(y => (string)y.Attribute("id") == "Id").FirstOrDefault(),
type = (string)x.Elements("field").Where(y => (string)y.Attribute("id") == "TransactionType").FirstOrDefault(),
balanse = (decimal)x.Elements("field").Where(y => (string)y.Attribute("id") == "Balance").FirstOrDefault(),
}).ToList();
XElement xStudentAccountTransactions = doc.Descendants("collection").Where(x => (string)x.Attribute("id") == "StudentAccountTransactions").FirstOrDefault();
var studentAccountTransactions = xStudentAccountTransactions.Elements("record")
.Select(x => new
{
satId = (string)x.Elements("field").Where(y => (string)y.Attribute("id") == "Id").FirstOrDefault(),
sId = (string)x.Elements("field").Where(y => (string)y.Attribute("id") == "StudentId").FirstOrDefault(),
tId = (string)x.Elements("field").Where(y => (string)y.Attribute("id") == "AccountTransactionsId").FirstOrDefault(),
}).ToList();
var studentRoll = students.Select(s => studentCollection.Where(sc => (s.sFName == sc.sFName) && (s.sLName == sc.sLName))
.Select(x => new { fName = s.sFName, lName = s.sLName, roll = s.rollNumber, sId = x.sId }).FirstOrDefault()).ToList();
var tranactions = accountTransactions.Select(at => studentAccountTransactions.Where(act => at.tId == act.tId)
.Select(x => new { tId = x.tId, type = at.type, balance = at.balanse, satId = x.satId }).FirstOrDefault()).ToList();
var results = (from sat in studentAccountTransactions
join tr in tranactions on sat.tId equals tr.tId
join sr in studentRoll on sat.sId equals sr.sId
select new { sr = sr, tr = tr, sat = sat })
.ToList();
foreach(var r in results)
{
dt.Rows.Add(new object[]
{
r.sr.sId,
r.sr.fName,
r.sr.lName,
r.sr.roll,
r.tr.tId,
r.sat.satId
});
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.