我有以下方法,我需要返回一个XElement
。问题是,无法弄清楚如何将byte[]
转换为XElement
。需要方法返回XElement而不是字节[]
public XElement ServiceCallForNewMadidPlantReport()
{
var kpiReportClient = new ReportExecutionService();
kpiReportClient.Credentials = System.Net.CredentialCache.DefaultCredentials;
var endPoint = new EndpointAddress("*****");
kpiReportClient.Url = endPoint.ToString();
//render arguments
byte[]result = null;
string reportSickTimePlants = "/Human Resources/KPI POW/Kpi_Fmla_Summarized_Plants_Prg"; //report path
string format = "XML";
string historyID = null;
string devInfo = "";
//prepare report parameter
ParameterValue[] parameters = new ParameterValue[4];
parameters[0] = new ParameterValue();
parameters[0].Name = "Location";
parameters[0].Value = "4"; //New Madrid
parameters[1] = new ParameterValue();
parameters[1].Name = "UnCode";
parameters[1].Value = "Non-Union";
parameters[2] = new ParameterValue();
parameters[2].Name = "StartDate";
parameters[2].Value = "01/01/2006";
parameters[3] = new ParameterValue();
parameters[3].Name = "EndDate";
parameters[3].Value = "08/08/2012";
string encoding;
string mimeType;
string extension;
Warning[] warnings = null;
string[] streamIDs;
ExecutionInfo execInfo = new ExecutionInfo();
ExecutionHeader execHeader = new ExecutionHeader();
kpiReportClient.ExecutionHeaderValue = execHeader;
var trustedUserHeader = new TrustedUserHeader();
execInfo = kpiReportClient.LoadReport(reportSickTimePlants, historyID);
kpiReportClient.SetExecutionParameters(parameters, "en-us");
String SessionId = kpiReportClient.ExecutionHeaderValue.ExecutionID;
Console.WriteLine("SessionID: {0}", kpiReportClient.ExecutionHeaderValue.ExecutionID);
try
{
result = kpiReportClient.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
Console.WriteLine(result);
execInfo = kpiReportClient.GetExecutionInfo();
}
catch (SoapException e)
{
Console.WriteLine(e.Detail.OuterXml);
}
try
{
FileStream sickTimReportForPlants = File.Create("SickTimeNonRepEmpReport.xml", result.Length);
Console.WriteLine("File created.");
Console.WriteLine(sickTimReportForPlants);
sickTimReportForPlants.Write(result, 0, result.Length);
Console.WriteLine("Result written to the file.");
sickTimReportForPlants.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
return ? (must be XElement)
}
这是我已经试过,但它不工作:
这给了我下面的错误:
System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.
我尝试了其他几个方面也但无论我做什么,我都会得到同样的错误。这里是xml文件的样子:
<?xml version="1.0" encoding="utf-8"?><Report xsi:schemaLocation="Kpi_Fmla_Summarized_Plants_Prg http://gonzo/ReportServer?%2fHuman+Resources%2fKPI+POW%2fKpi_Fmla_Summarized_Plants_Prg&rs%3aFormat=XML&rc%3aSchema=True" Name="Kpi_Fmla_Summarized_Plants_Prg" Location="New Madrid" Textbox84="KPI SICK/FMLA 1/1/2006 12:00:00 AM - 8/8/2012 12:00:00 AM" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="Kpi_Fmla_Summarized_Plants_Prg"><Tablix1><Textbox13><Textbox3><Textbox15 /></Textbox3></Textbox13><Category_Collection><Category Category1="FMLA"><represented1_Collection><represented1 represented11="Non-Union"><QTR_Collection><QTR QTR1="1" Textbox27="1787" /><QTR QTR1="2" Textbox27="988.75" /><QTR QTR1="3" Textbox27="1577.75" /><QTR QTR1="4" Textbox27="2133.25" /></QTR_Collection></represented1></represented1_Collection></Category><Category Category1="Sick"><represented1_Collection><represented1 represented11="Non-Union"><QTR_Collection><QTR QTR1="1" Textbox27="5407.5" /><QTR QTR1="2" Textbox27="4051.75" /><QTR QTR1="3" Textbox27="3928" /><QTR QTR1="4" Textbox27="4204.75" /></QTR_Collection></represented1></represented1_Collection></Category></Category_Collection></Tablix1></Report>
我不知道我在做什么错。
编辑:
后做了以下内容:
string resultFromBytes = UTF8Encoding.UTF8.GetString(result);
return XDocument.Parse(resultFromBytes).Root;
我仍然得到同样的错误信息,但这里是resultFromBytes我的XML
<?xml version="1.0" encoding="utf-8"?><Report xsi:schemaLocation="Kpi_Fmla_Summarized_Plants_Prg http://gonzo/ReportServer?%2fHuman+Resources%2fKPI+POW%2fKpi_Fmla_Summarized_Plants_Prg&rs%3aFormat=XML&rc%3aSchema=True" Name="Kpi_Fmla_Summarized_Plants_Prg" Location="New Madrid" Textbox84="KPI SICK/FMLA 1/1/2006 12:00:00 AM - 8/8/2012 12:00:00 AM" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="Kpi_Fmla_Summarized_Plants_Prg"><Tablix1><Textbox13><Textbox3><Textbox15 /></Textbox3></Textbox13><Category_Collection><Category Category1="FMLA"><represented1_Collection><represented1 represented11="Non-Union"><QTR_Collection><QTR QTR1="1" Textbox27="1787" /><QTR QTR1="2" Textbox27="988.75" /><QTR QTR1="3" Textbox27="1577.75" /><QTR QTR1="4" Textbox27="2133.25" /></QTR_Collection></represented1></represented1_Collection></Category><Category Category1="Sick"><represented1_Collection><represented1 represented11="Non-Union"><QTR_Collection><QTR QTR1="1" Textbox27="5407.5" /><QTR QTR1="2" Textbox27="4051.75" /><QTR QTR1="3" Textbox27="3928" /><QTR QTR1="4" Textbox27="4204.75" /></QTR_Collection></represented1></represented1_Collection></Category></Category_Collection></Tablix1></Report>
编辑:
后这样做:
string resultFromBytes = UTF8Encoding.UTF8.GetString(result);
var regex = new System.Text.RegularExpressions.Regex(@"xsi:schemaLocation="".+""");
string strippedxml = regex.Replace(resultFromBytes, "");
return XDocument.Parse(strippedxml).Root;
我得到同样的失败。这是strippedxml的样子:
<?xml version="1.0" encoding="utf-8"?><Report /></QTR_Collection></represented1></represented1_Collection></Category></Category_Collection></Tablix1></Report>
编辑:
尝试这样做,XML看起来好多了,但还是失败,同样的错误:
string resultFromBytes = UTF8Encoding.UTF8.GetString(result);
var regex = new System.Text.RegularExpressions.Regex(@"xsi:schemaLocation="".+""");
string strippedxml = regex.Replace(resultFromBytes, "");
return XDocument.Parse(strippedxml).Root;
这里就是从strippedxml的XML是什么样子:
<?xml version="1.0" encoding="utf-8"?><Report Name="Kpi_Fmla_Summarized_Plants_Prg" Location="New Madrid" Textbox84="KPI SICK/FMLA 1/1/2006 12:00:00 AM - 8/8/2012 12:00:00 AM" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="Kpi_Fmla_Summarized_Plants_Prg"><Tablix1><Textbox13><Textbox3><Textbox15 /></Textbox3></Textbox13><Category_Collection><Category Category1="FMLA"><represented1_Collection><represented1 represented11="Non-Union"><QTR_Collection><QTR QTR1="1" Textbox27="1787" /><QTR QTR1="2" Textbox27="988.75" /><QTR QTR1="3" Textbox27="1577.75" /><QTR QTR1="4" Textbox27="2133.25" /></QTR_Collection></represented1></represented1_Collection></Category><Category Category1="Sick"><represented1_Collection><represented1 represented11="Non-Union"><QTR_Collection><QTR QTR1="1" Textbox27="5407.5" /><QTR QTR1="2" Textbox27="4051.75" /><QTR QTR1="3" Textbox27="3928" /><QTR QTR1="4" Textbox27="4204.75" /></QTR_Collection></represented1></represented1_Collection></Category></Category_Collection></Tablix1></Report>
你为什么试图将其解析为base-64?试试'XElement.Parse(result)' – 2012-08-08 18:45:05
@JohnSaunders这是我尝试做的第一件事。 XElement.Parse接受一个字符串,结果是一个byte []类型,所以它给了我一个错误。我试图让结果是一个字符串[]而不是一个字节[],但是如果我理解了MSDN,ReportExecutionService.Render()必须是一个字节[] – 2012-08-08 18:53:28
@JohnSaunders I misspoke。我第一次尝试使用XElement.Parse(result),但它不会接受它,因为它必须是一个字符串。然后我尝试了XElement.Parse(结果。ToString())和我收到了与其他所有人收到的错误。我试图将其解析为基础64,因为当我使用Google时,这是解决类似问题的方法。 – 2012-08-08 19:09:44