C# 从XML文件中获取数据
我需要写入一个异常表。因此,如果UploadStatus=Failed,我需要从CDR_STATUS元素中获取ClientLoanNumber和UploadedDt,然后从CDR_ERROR元素中获取后面的内容。因此,我认为CDR_状态有一个主循环,当条件UploadStatus=Failed时,它会循环通过CDR_状态元素。我需要返回ClientLoanNumber、UploadedDt和来自CDR_Status元素的消息 以下是提供者的实际响应。格式很糟糕。很抱歉我是c开发人员,所以正在寻找c。谢谢你的帮助C# 从XML文件中获取数据,c#,xml,C#,Xml,我需要写入一个异常表。因此,如果UploadStatus=Failed,我需要从CDR_STATUS元素中获取ClientLoanNumber和UploadedDt,然后从CDR_ERROR元素中获取后面的内容。因此,我认为CDR_状态有一个主循环,当条件UploadStatus=Failed时,它会循环通过CDR_状态元素。我需要返回ClientLoanNumber、UploadedDt和来自CDR_Status元素的消息 以下是提供者的实际响应。格式很糟糕。很抱歉我是c开发人员,所以正在寻找
<?xml version="1.0" encoding="UTF-8"?>
<CWL_DATA>
<CWL_RESPONSE RequestType="CDRSearch" ClientID="11559" ResponseDateTime="6/26/2018 2:43:31 PM">
<CRITERIA ReferenceNumber="412497" DateFrom="6/26/2018" DateTo="" UploadFileName="" />
<RESPONSE_DATA>
<CDR_STATUS UploadStatus="Failed" CWLLoanNumber="10050764" ClientLoanNumber="7000904180" FileName="WebService6/26/2018 2:43:25 PM" UploadedDt="6/26/2018 2:43:25 PM" BorrowerLastName="Beeching" LoanAmt="146520.00" LineType="Repo" RequestType="Wire" LoanStatus="Suspended" >
<CDR_ERROR Message="Loan Number and / or Date Time stamp is not valid. Please download the excel file again and re-import using the new excel file."/>
<CDR_ERROR Message="MFR Units is invalid."/>
<CDR_ERROR Message="Please enter MIP% as a decimal number between 0.001 and 100 with up to 3 decimal digits."/>
</CDR_STATUS> <CDR_STATUS UploadStatus="Failed" CWLLoanNumber="10050766" ClientLoanNumber="7000907217" FileName="WebService6/26/2018 2:43:25 PM" UploadedDt="6/26/2018 2:43:25 PM" BorrowerLastName="Ginsburg" LoanAmt="69000.00" LineType="Repo" RequestType="Wire" LoanStatus="Suspended" >
<CDR_ERROR Message="Loan Number and / or Date Time stamp is not valid. Please download the excel file again and re-import using the new excel file."/>
<CDR_ERROR Message="MFR Units is invalid."/>
<CDR_ERROR Message="Base Loan Amount is not required for loans with Loan Type other than `FHA', 'CL FHA', 'VA' and 'CL VA'."/>
</CDR_STATUS> <CDR_STATUS UploadStatus="Failed" CWLLoanNumber="10050765" ClientLoanNumber="7000907225" FileName="WebService6/26/2018 2:43:25 PM" UploadedDt="6/26/2018 2:43:25 PM" BorrowerLastName="Cesaro" LoanAmt="83838.00" LineType="Repo" RequestType="Wire" LoanStatus="Suspended" >
<CDR_ERROR Message="Loan Number and / or Date Time stamp is not valid. Please download the excel file again and re-import using the new excel file."/>
<CDR_ERROR Message="MFR Units is invalid."/>
<CDR_ERROR Message="Please enter MIP% as a decimal number between 0.001 and 100 with up to 3 decimal digits."/>
</CDR_STATUS> <CDR_STATUS UploadStatus="Pending" CWLLoanNumber="10050767" ClientLoanNumber="7000908314" FileName="WebService6/26/2018 2:43:25 PM" UploadedDt="6/26/2018 2:43:25 PM" BorrowerLastName="Massari" LoanAmt="191919.00" LineType="Repo" RequestType="Wire" LoanStatus="Suspended" >
</CDR_STATUS> <CDR_STATUS UploadStatus="Pending" CWLLoanNumber="10050768" ClientLoanNumber="7000909833" FileName="WebService6/26/2018 2:43:25 PM" UploadedDt="6/26/2018 2:43:25 PM" BorrowerLastName="Varney" LoanAmt="76312.00" LineType="Repo" RequestType="Wire" LoanStatus="Suspended" >
</CDR_STATUS> <CDR_STATUS UploadStatus="Pending" CWLLoanNumber="10050769" ClientLoanNumber="7000911722" FileName="WebService6/26/2018 2:43:25 PM" UploadedDt="6/26/2018 2:43:25 PM" BorrowerLastName="Lawson Cousins" LoanAmt="290000.00" LineType="Repo" RequestType="Wire" LoanStatus="Suspended" >
</CDR_STATUS> <CDR_STATUS UploadStatus="Pending" CWLLoanNumber="10050770" ClientLoanNumber="7000911763" FileName="WebService6/26/2018 2:43:25 PM" UploadedDt="6/26/2018 2:43:25 PM" BorrowerLastName="Ball" LoanAmt="207570.00" LineType="Repo" RequestType="Wire" LoanStatus="Suspended" >
</CDR_STATUS> <CDR_STATUS UploadStatus="Pending" CWLLoanNumber="10050771" ClientLoanNumber="7000914155" FileName="WebService6/26/2018 2:43:25 PM" UploadedDt="6/26/2018 2:43:25 PM" BorrowerLastName="Rauf" LoanAmt="404700.00" LineType="Repo" RequestType="Wire" LoanStatus="Suspended" >
</CDR_STATUS> <CDR_STATUS UploadStatus="Pending" CWLLoanNumber="10050773" ClientLoanNumber="7000915947" FileName="WebService6/26/2018 2:43:25 PM" UploadedDt="6/26/2018 2:43:25 PM" BorrowerLastName="Dino" LoanAmt="245471.00" LineType="Repo" RequestType="Wire" LoanStatus="Suspended" >
</CDR_STATUS> </RESPONSE_DATA>
</CWL_RESPONSE>
</CWL_DATA>
我将结果放入数据表:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("ClientLoanNumber", typeof(string));
dt.Columns.Add("UploadedDt", typeof(DateTime));
dt.Columns.Add("Error", typeof(string));
XDocument doc = XDocument.Load(FILENAME);
List<XElement> failed = doc.Descendants("CDR_STATUS").Where(x => (string)x.Attribute("UploadStatus") == "Failed").ToList();
foreach (var fail in failed)
{
DataRow newRow = dt.Rows.Add();
newRow["ClientLoanNumber"] = (string)fail.Attribute("CWLLoanNumber");
newRow["UploadedDt"] = DateTime.ParseExact((string)fail.Attribute("UploadedDt"), "M/dd/yyyy h:mm:ss tt", System.Globalization.CultureInfo.InvariantCulture);
newRow["Error"] = string.Join("\n", fail.Elements("CDR_ERROR").Select(x => (string)x.Attribute("Message")));
}
}
}
}
这就是我做的
DataTable resultsDT = new DataTable();
resultsDT.Columns.Add("LoanNUmber");
resultsDT.Columns.Add("Error");
XDocument X = XDocument.Load(pathStatusRequestResult);
var status = X.Element("CWL_DATA").Element("CWL_RESPONSE");
var statusData = status.Element("RESPONSE_DATA").Elements("CDR_STATUS");
foreach(var item in statusData)
{
// returns the value of teh UploadStatus
bool uploadStatus = (item.Attribute("UploadStatus").Value) == "Failed" ? false : true;
string loanNumber = item.Attribute("ClientLoanNumber").Value;
if (uploadStatus == false)
{
var errorData = item.Elements("CDR_ERROR");
foreach(var e in errorData)
{
string error = e.Attribute("Message").Value;
resultsDT.Rows.Add(loanNumber, error);
}
}
}
如果您试图编写代码,但遇到特定问题,我们可以帮助您,但我们不是来从头开始编写代码的-请参阅同意@JohnM。请进行一些代码测试您是否正在将实际客户数据转储到公共网站上…?不,我仅使用测试数据如果您已经解决了问题,您可以接受自己的答案以向其他人表明问题已经解决:看起来你拿走了我的一些代码,但是你没有得到你要求的日期。实际上没有。我确实找到了一些我利用的示例代码。然后我将元素放入字符串中,直到我知道如何获取所需的项目。棘手的部分是第二圈。