在 C# 语句中使用 LINQ 查询多个 XML 文件
我正在尝试使用 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?
接受任何和所有建议。
I'm attempting to extract data from a number of XML files posted on the Internet using LINQ. I'm working with LINQPad and using C# Statements. All files have the same format and Element names. My goal is to extract the same Elements from each file and then report the elements in one row per file, creating a grid of sorts. This would then ideally be exported to excel. I'm new to LINQ so any help would be greatly appreciated.
Below is my working code:
// 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);
}
How do I get the queried elements to appear horizontal and with some delimiter?
Currently my code only works for 1 XML file. How can this be altered to loop for multiple files? The source file names all have the same base URL with the only difference being the ending statement. Is there a way to make the Load reference the base URL concatenated with a variable list which would contain the ending statement?
Open to any and all suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
看来你为此付出了一些努力,所以向你致敬。我从您的代码中发现了一些初学者 LINQ 错误或误解,并希望在下面解决它们。
后代
。请改用Element
。从“Pool Num”到“WA CAP”的每一个都是一个独立的 XML 元素,可以通过以下方式直接检索:parent.Element(p + "PoolNumber")
其中parent< /code> 是所需元素的父元素。
Value
属性:parent.Element(p + "PoolNumber").Value
。使用(string)
转换并不是不正确的,但是当您怀疑该元素可能存在或不存在时,最好使用它。如果它不存在,调用Value
将返回NullReferenceException
,因为它将为 null。铸造它可以解决这个问题。在下面的代码中测试这一点的简单方法是在pool
声明之后添加一个pool.Element(p + "PoolNumber").Remove();
并观察它休息。然后使用您的(string)
方法并看着它愉快地继续。Element
方法有效地取代了仅为了获取一个值而对结果进行foreach
的需要。我建议尝试使用First
、Single
、FirstOrDefault
和SingleOrDefault
方法。您有 LINQPad,因此请查看示例并使用它们。除此之外,您的局部变量名称应按照标准格式期望以小写字母开头。将 LINQ 方法调用排列在单独的行上并在点表示法的开头对齐也很有帮助。
使用
String.Join
方法。在 .NET 4.0 中,无需调用ToArray
,因为该方法接受IEnumerable
的重载。将池编号值放入列表中,然后对其进行 foreach 并将逻辑放入循环体中。请参阅下面的我的代码。
这是代码的清理版本。我不确定您是否希望所有标题都是水平的,还是只关心在具有多个值的项目上使用分隔符。
It looks like you put some decent effort into this, so hats off to you. I recognize some beginner LINQ mistakes or misunderstandings from your code and hope to address them below.
Descendants
. UseElement
instead. Everyting from "Pool Num" to "WA CAP" is a stand-alone XML element that can be retrieved directly in this manner:parent.Element(p + "PoolNumber")
whereparent
is the desired element's parent element.Value
property:parent.Element(p + "PoolNumber").Value
. Using the(string)
cast is not incorrect, however it's preferable to use it when you suspect that the element may or may not exist. If it doesn't exist callingValue
would return aNullReferenceException
since it would be null. Casting it gets around this. Easy way to test this in my code below is to add apool.Element(p + "PoolNumber").Remove();
after the declaration ofpool
and watch it break. Then use your(string)
approach and watch it happily continue.Element
approach effectively replaces the need toforeach
over the result just to get one value. I recommend playing around withFirst
,Single
,FirstOrDefault
andSingleOrDefault
methods. You have LINQPad so check out the examples and play with them.Apart from that, your local variable names should begin with a lowercase per the standard formatting expectations. It's also helpful to line up your LINQ method calls on separate lines and align them at the start of the dot notation.
Use the
String.Join
method. With .NET 4.0 there's no need to callToArray
since the method accepts an overload forIEnumerable<string>
.Place your pool number values in a list then foreach over it and place the logic in the body of the loop. See my code below.
Here's the cleaned up version of your code. I wasn't sure whether you wanted all the headers to be horizontal or were only concerned with using a delimiter on items with multiple values.
我在查看 Stack Overflow 上的另一个问题时看到了这个问题,并注意到这个问题仍然没有答案。我不知道这对您来说是否仍然是一个问题,但对于阅读本文的其他人来说,这是对多个源执行 LINQ 查询的一种方法。
诀窍是使用 [Enumerable.Concat(TSource) 方法][1]。
我希望这对您或任何人有帮助。
[1]:http://msdn.microsoft.com/en-us/library /bb302894.aspx Enumerable.Concat(TSource) 方法
I saw this question while looking at another question on Stack Overflow, and noticed that this one is still unanswered. I don't know if this is still an issue for you, but for anyone else reading this, this is one way to perform an LINQ query towards several sources.
The trick is to use the [Enumerable.Concat(TSource) Method][1].
I hope this helps you or anyone.
[1]: http://msdn.microsoft.com/en-us/library/bb302894.aspx Enumerable.Concat(TSource) Method