我試圖從使用LINQ在Internet上發佈的一些XML文件中提取數據。我正在使用LINQPad並使用C#語句。所有文件都具有相同的格式和元素名稱。我的目標是從每個文件中提取相同的元素,然後爲每個文件在一行中報告元素,創建一個排序網格。這將理想地被導出爲excel。我是LINQ的新手,所以任何幫助將不勝感激。 下面是我的工作代碼:使用LINQ在C#語句中查詢多個XML文件
// 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與一個變量列表連接在一起,後者將包含結束語句?
任何和所有的建議。