0

我有以下方法需要返回一个XElement. 问题是无法弄清楚如何将 abyte[]转换为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)
}

这是我尝试过的,但它不起作用:

result = kpiReportClient.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
Console.WriteLine(result);
string resultString  = Convert.ToBase64String(result);
var nonRepSickReportForNM = XElement.Parse(resultString);

这给了我以下错误:

System.Xml.XmlException:根级别的数据无效。第 1 行,位置 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&amp;rs%3aFormat=XML&amp;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&amp;rs%3aFormat=XML&amp;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>
4

2 回答 2

2

我有答案。我试图将它从 byte[] 转换为 XElement。相反,我将其转换为流,然后转换为 Xelement。像这样:

result = kpiReportClient.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
Console.WriteLine(result);
result = kpiReportClient.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
Stream resStream = new MemoryStream(result);
var nonRepSickReportForNM = XElement.Load(resStream);
return nonRepSickReportForNM

不过,我不能把它归功于它。我的老板想通了。

于 2012-08-08T18:42:43.637 回答
1

也许是这样的:(修复了命名空间问题)

string resultFromBytes = UTF8Encoding.UTF8.GetString(result);

var nt = new NameTable();
var nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace(String.Empty, "urn:samples"); //default namespace
nsmgr.AddNamespace("xsi", "urn:samples2"); // xsi fix

var context = new XmlParserContext(nt, nsmgr,null,  XmlSpace.None);

var settings = new XmlReaderSettings();
settings.ConformanceLevel = ConformanceLevel.Document;

var xrdr= XmlReader.Create(new StringReader(xml), settings, context);
return XDocument.Load(xrdr).Root;
于 2012-08-08T16:35:30.077 回答