在Intuit Quickbooks的C#SDK帮助下,我得到了api调用PNL报告的响应。但我想在xml文件中保存此响应。
我目前的数据SerializationFormat是Json。我试图将json更改为Xml,但它会给出错误。
private void GetReportAndPrintToConsole(ServiceContext qboServiceContext, String reportName, String startDate, String endDate)
{
//JSON required for QBO Reports API
qboServiceContext.IppConfiguration.Message.Request.SerializationFormat = Intuit.Ipp.Core.Configuration.SerializationFormat.Json; qboServiceContext.IppConfiguration.Message.Response.SerializationFormat = Intuit.Ipp.Core.Configuration.SerializationFormat.Json;
//Instantiate ReportService
ReportService reportsService = new ReportService(qboServiceContext);
//Set properties for Report
reportsService.start_date = startDate;
reportsService.end_date = endDate;
//Execute Report API call
Intuit.Ipp.Data.Report report = reportsService.ExecuteReport(reportName);
Console.WriteLine(report.GetType());
答案 0 :(得分:0)
所以我想以XML格式保存损益报告,但是我从api获得了Report
对象,所以我必须手动编写代码来获取XML。
Here是任何人都可以使用的代码段,用于将Report
对象转换为XML字符串。
在此行之后
Intuit.Ipp.Data.Report report = reportsService.ExecuteReport(reportName);
像这样调用函数
string xmlstring = getReportInXML(report);
或直接从此处复制粘贴
#region Get XML From Report object
/*==================================================
= Get XML From Report object =
==================================================*/
private string getReportInXML(Report report)
{
string strreport = "";
strreport += "<Report xmlns=\"http://schema.intuit.com/finance/v3\">";
strreport += getReportHeader(report.Header);
strreport += getReportColumns(report.Columns);
strreport += getRows(report.Rows);
strreport += "</Report>";
return strreport;
}
private string getReportHeader(ReportHeader header)
{
string strheader = "";
strheader += "<Header>";
strheader += "<Time>" + String.Format("{0:yyyy-MM-ddTH:mm:sszzz}", header.Time) + "</Time>";
strheader += "<ReportName>" + header.ReportName + "</ReportName>";
strheader += "<DateMacro>" + header.DateMacro + "</DateMacro>";
strheader += "<ReportBasis>" + header.ReportBasis + "</ReportBasis>";
strheader += "<StartPeriod>" + header.StartPeriod + "</StartPeriod>";
strheader += "<EndPeriod>" + header.EndPeriod + "</EndPeriod>";
strheader += "<SummarizeColumnsBy>" + header.SummarizeColumnsBy + "</SummarizeColumnsBy>";
strheader += "<Currency>" + header.Currency + "</Currency>";
foreach (var option in header.Option)
{
strheader += "<Option>";
strheader += "<Name>" + option.Name + "</Name>";
strheader += "<Value>" + option.Value + "</Value>";
strheader += "</Option>";
}
strheader += "</Header>";
return strheader;
}
private string getReportColumns(Column[] columns)
{
string strcolumn = "";
strcolumn += "<Columns>";
foreach (Column column in columns)
{
strcolumn += "<Column>";
strcolumn += " <ColTitle>" + Convert.ToString(column.ColTitle) + "</ColTitle>";
strcolumn += " <ColType>" + column.ColType + "</ColType>";
foreach (var meta in column.MetaData)
{
strcolumn += "<MetaData>";
strcolumn += "<Name>" + meta.Name + "</Name>";
strcolumn += "<Value>" + meta.Value + "</Value>";
strcolumn += "</MetaData>";
}
strcolumn += "</Column>";
}
strcolumn += "</Columns>";
return strcolumn;
}
private string getRows(Row[] rows)
{
string strrows = "";
strrows += " <Rows>";
foreach (Row row in rows)
{
if (row.type.ToString() == "Section") ///It contains more Rows
{
// if type of row is data then call it
strrows += getRowSection(row);
}
else if (row.type.ToString() == "Data")
{
//Otherwise call getRowSection
strrows += getRowData(row);
}
}
strrows += " </Rows>";
return strrows;
}
private string getRowSection(Row row)
{
string strrowsection = "";
strrowsection += " <Row type=\"Section\"";
if (row.group != null)
{
strrowsection += " group=\"" + row.group + "\"";
}
strrowsection += ">";
string type = row.AnyIntuitObjects[0].GetType().ToString();
object[] objs = (object[])row.AnyIntuitObjects;
foreach (var obj in objs)
{
string objtype = obj.GetType().ToString();
switch (objtype)
{
case "Intuit.Ipp.Data.Header":
Header header = (Header)obj;
strrowsection += getHeader(header);
break;
case "Intuit.Ipp.Data.Rows":
Rows rows = (Rows)obj;
strrowsection += getRows(rows.Row);
break;
case "Intuit.Ipp.Data.Summary":
Summary summary = (Summary)obj;
strrowsection += getSummary(summary);
break;
}
}
strrowsection += " </Row>";
return strrowsection;
}
private string getSummary(Summary summary)
{
string strsummary = "";
strsummary += "<Summary>";
ColData[] coldata = (ColData[])summary.ColData;
strsummary += getColData(coldata);
strsummary += "</Summary>";
return strsummary;
}
private string getRowData(Row row)
{
string strrowdata = "";
strrowdata += " <Row type=\"Data\">";
ColData[] coldata = (ColData[])row.AnyIntuitObjects[0];
strrowdata += getColData(coldata);
strrowdata += "</Row>";
return strrowdata;
}
private string getHeader(Header header)
{
string strheader = "";
strheader += " <Header>";
ColData[] coldata = (ColData[])header.ColData;
strheader += getColData(coldata);
strheader += " </Header>";
return strheader;
}
private string getColData(ColData[] coldata)
{
string strcoldata = "";
foreach (ColData col in coldata)
{
//Generate the coldata string here.
string value = col.value;
value = value.Replace("&", "&");
strcoldata += "<ColData value=\"" + value + "\"";
if (col.id != null)
{
strcoldata += " id=\"" + col.id + "\"";
}
strcoldata += " /> ";
}
return strcoldata;
}
/*===== End of Get XML From Report object ======*/
#endregion End of Get XML From Report object