提问人:A_developer 提问时间:7/14/2022 最后编辑:A_developer 更新时间:7/14/2022 访问量:229
根据 c 中的子节点数据筛选 XML 集合#
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>
students 节点是父节点,它由子节点 as 和集合(StudentCollection、AccountTransactions 和 StudentAccountTransactions)组成。
<stdXmlFormat version="1.0">
<students>
<student></student>
<collection id="StudentCollection"/>
<collection id="AccountTransactions"/>
<collection id="StudentAccountTransactions"/>
</students>
</stdXmlFormat>
学生节点包含所有学生详细信息,例如名字,姓氏,卷号等(这是一个包含更多信息的旧集合,为了更好地阅读和理解,我删除了一些属性)
新集合 StudentCollection 中提供了相同的信息,但数据较少。此集合具有 StudentId(用于标识其他集合中的学生的唯一 guid)。与 students 和 StudentCollection 的唯一关系是学生的名字和姓氏。
AccountTransactions 是保存 id(用于标识每个事务的唯一 guid)和其他事务详细信息的集合。此集合中用作 StudentAccountTransactions 集合中的 AccountTransactionsId 的 id。
最后一个集合 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。
出于测试目的,我尝试使用以下方式过滤 studentid 集合,但没有奏效。
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
});
}
}
}
}
评论