我正在尝试使用 LINQ 从 Internet 上发布的许多 XML 文件中提取数据。我正在使用 LINQPad 并使用 C# 语句。所有文件都具有相同的格式和元素名称。我的目标是从每个文件中提取相同的元素,然后在每个文件的一行中报告元素,创建一个排序网格。理想情况下,这将被导出到excel。我是 LINQ 的新手,因此我们将不胜感激任何帮助。以下是我的工作代码:
// Load From Website.
XElement Tags=XElement.Load("http://fapt.efanniemae.com/epooltalk-hvd/pool.xml?type=XML&pn="+"510299"+".XML");
//XElement Tags=XElement.Load("http://fapt.efanniemae.com/epooltalk-hvd/pool.xml?type=XML&pn="+(list1)+".XML");
XNamespace p = "http://fapt.efanniemae.com";
/Run Export
var titles =
from book in Tags.Descendants(p + "Pool")
let bookAttributes = book.Element(p + "PoolFactors")
let title = ((string)book.Element(p + "PoolNumber"))
let title2 = ((string)bookAttributes.Element(p + "PoolFactor"))
let month = (string)bookAttributes.Element (p + "Month")
group title by month;
foreach (var group in titles) {
foreach (var title in group) {
Console.WriteLine("Pool Num |" + title);
}
}
foreach(XElement CusipElement in Tags.Descendants(p + "CUSIP")) {
Console.WriteLine("CUSIP |" +(string)CusipElement);
}
foreach(XElement PrefixElement in Tags.Descendants(p + "PoolPrefix")) {
Console.WriteLine("PoolPrefix |" +(string)PrefixElement);
}
foreach(XElement ObalElement in Tags.Descendants(p + "OriginalSecurityBalance")) {
Console.WriteLine("Orig. Bal |" +(string)ObalElement);
}
foreach(XElement OtermElement in Tags.Descendants(p + "WeightedAverageOrigLoanTerm")) {
Console.WriteLine("Orig. Term |" +(string)OtermElement);
}
foreach(XElement RtermElement in Tags.Descendants(p + "WAMnthsRemainingToAmortization")) {
Console.WriteLine("Remain Term |" +(string)RtermElement);
}
foreach(XElement WalaElement in Tags.Descendants(p + "WeightedAverageLoanAge")) {
Console.WriteLine("WALA |" +(string)WalaElement);
}
foreach(XElement AccrateElement in Tags.Descendants(p + "CurrentAccrualRate")) {
Console.WriteLine("Net Rate |" +(string)AccrateElement);
}
foreach(XElement MarginElement in Tags.Descendants(p + "WeightedAverageLoanMarginRate")) {
Console.WriteLine("WA Margin |" +(string)MarginElement);
}
foreach(XElement SubtElement in Tags.Descendants(p + "SubType")) {
Console.WriteLine("SubType |" +(string)SubtElement);
}
//foreach(XElement MonthElement in Tags.Descendants(p + "Month"))
//foreach(XElement WacElement in Tags.Descendants(p + "WAC")) {
//Console.WriteLine("WAC |" +(string)WacElement + "|" +(string)MonthElement);
//}
foreach(XElement UpdatedcapElement in Tags.Descendants(p + "UpdatedCap")) {
Console.WriteLine("Updated CAP |" +(string)UpdatedcapElement);
}
foreach(XElement IdateElement in Tags.Descendants(p + "IssueDate")) {
Console.WriteLine("Issue Date |" +(string)IdateElement);
}
foreach(XElement MdateElement in Tags.Descendants(p + "MaturityDate")) {
Console.WriteLine("Maturity Date |" +(string)MdateElement);
}
foreach(XElement RadjElement in Tags.Descendants(p + "RateAdjustmentFrequency")) {
Console.WriteLine("Rate Adj Freq |" +(string)RadjElement);
}
foreach(XElement PcapElement in Tags.Descendants(p + "PerAdjustmentCap")) {
Console.WriteLine("Period Cap |" +(string)PcapElement);
}
foreach(XElement PchgfreqElement in Tags.Descendants(p + "PaymentChangeFrequency")) {
Console.WriteLine("Pymt Chg Freq |" +(string)PchgfreqElement);
}
foreach(XElement MtrElement in Tags.Descendants(p + "WeightedAverageMonthsToRoll")) {
Console.WriteLine("WA MTR |" +(string)MtrElement);
}
foreach(XElement RatecapElement in Tags.Descendants(p + "WeightedAverageCap")) {
Console.WriteLine("WA CAP |" +(string)RatecapElement);
}
var Months = Tags.Descendants(p + "Month")
.Select(titleElement => (string)titleElement);
foreach (string title in Months) {
Console.WriteLine("Months |" + title);
}
var Wacs = Tags.Descendants(p + "WAC")
.Select(titleElement => (string)titleElement);
foreach (string title in Wacs) {
Console.WriteLine("WAC |" + title);
}
var Wams = Tags.Descendants(p + "WAM")
.Select(titleElement => (string)titleElement);
foreach (string title in Wams) {
Console.WriteLine("WAM |" + title);
}
var Factors = Tags.Descendants(p + "Factor")
.Select(titleElement => (string)titleElement);
foreach (string title in Factors) {
Console.WriteLine("Factor |" + title);
}
如何让查询的元素显示为水平并带有一些分隔符?
目前我的代码仅适用于 1 个 XML 文件。如何将其更改为循环多个文件?源文件名都具有相同的基本 URL,唯一的区别是结束语句。有没有办法让 Load 引用与包含结束语句的变量列表连接的基本 URL?
接受任何和所有建议。