本文介绍了如何将 Quickoboks 在线 api 报告对象转换为 xml 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Intuit Quickbooks 在线 C# SDK 的帮助下,我收到了对 PNL 报告的 api 调用的响应.但我想将此响应保存在 xml 文件中.

With the help of C# SDK of Intuit Quickbooks online I am getting the response of api call for PNL report. But I want to save this response in xml file.

我目前数据SerializationFormat是Json.我试图将 json 更改为 Xml,但它给出了错误.

I Currently data SerializationFormat is Json. I tried to change json to Xml but it gives the errors.

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

推荐答案

所以我想以 XML 格式保存损益报告,但我从 api 获取了 Report 对象,所以我必须手动编写代码以获取 XML.

So I wanted to save the Profit and Loss report in XML format but I was getting Report object from the api so I have to write the code manually to get the XML.

这里是任何人都可以用来转换的代码片段报告 对象转换成 XML 字符串.

Here is the code snippet that can be used by anyone to convert Report object into XML string.

在这一行之后

Intuit.Ipp.Data.Report report = reportsService.ExecuteReport(reportName);

这样调用函数

string xmlstring = getReportInXML(report);

或者直接从这里复制粘贴

Or directly copy paste from here

#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("&", "&amp;");
        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

这篇关于如何将 Quickoboks 在线 api 报告对象转换为 xml 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 18:15