Programming Newbie
Programming Newbie

Reputation: 1227

Need method to return XElement instead of byte[]

I have the following method that I need to return an XElement. The problem is that can't figure out how to convert a byte[] into an 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)
}

Here is what I have tried but it doesn't work:

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);

This gives me the following error:

System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1.

I've tried several other ways as well but no matter what I do I get the same error. Here is what the xml file looks like:

<?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>    

I'm not sure what I am doing wrong.

EDIT:

after doing the following:

string resultFromBytes = UTF8Encoding.UTF8.GetString(result);
return XDocument.Parse(resultFromBytes).Root;

I still get the same error message but here is my xml from resultFromBytes

<?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>

EDIT:

After doing this:

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;

I get the same failure. This is what the strippedxml looks like:

<?xml version="1.0" encoding="utf-8"?><Report  /></QTR_Collection></represented1></represented1_Collection></Category></Category_Collection></Tablix1></Report>

EDIT:

Tried this and xml looks much better but still fails with same error:

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;

here's what the xml from strippedxml looks like:

<?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>

Upvotes: 0

Views: 2582

Answers (2)

rene
rene

Reputation: 42483

Maybe something along the line of this: (fixed the namespace trouble)

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;

Upvotes: 1

Programming Newbie
Programming Newbie

Reputation: 1227

I have the answer. I was trying to converti it from a byte[] to an XElement. Instead, I converted it into a stream and THEN into the Xelement. Like this:

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

I can't take credit for it though. My boss figured it out.

Upvotes: 2

Related Questions