使用URL以JSON格式导出SSRS报告

时间:2019-06-17 10:15:32

标签: c# xml xslt asp.net-core ssrs-2012

在使用C#将SSRS XML报告转换为JSON的同时,在每个属性中获取“ @”作为前缀。 我们如何从每个属性中删除不需要的“ @”?

由于SSRS不支持,因此可以使用URL将其报告下载为JSON格式。因此,我首先以XML格式导出报告,然后将报告转换为JSON。

我正在使用Newtonsoft.JSON库将XML转换为JSON。

    //SSRS Report URL :- 
   _reportURL = http://<server-name>/reportserver?/<report- 
    name>/&rs:Format=Xml

    // Getting byte array using HttpClient
    _response = GetHttpClient().GetAsync(_reportURL).Result;

    if (_response.IsSuccessStatusCode)
    { 
      var data = _response.Content.ReadAsStreamAsync(); // Getting stream of 
      data from server response

      XmlDocument xmlDocument = new XmlDocument();
      //xml document by using Stream
      using (data.Result)
      {
        xmlDocument.Load(data.Result);
      }

      // Deserializing xml node to json string
      string jsonString = JsonConvert.SerializeXmlNode(xmlDocument, 
      Newtonsoft.Json.Formatting.Indented, false);
      if (!jsonString.Equals(String.Empty)){
        // Getting byte array string      
        byteArray = Encoding.ASCII.GetBytes(jsonString); 
      }
    }

Actual Result :-
{
  "?xml": {
    "@version": "1.0",
    "@encoding": "utf-8"
  },
  "Report": {    
    "@Name": "<report-name>",
    "@xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "@xmlns": "<report-name>",
    "Tablix1": {
      "Details_Collection": {
        "Details": [
          {
            "@CompanyId": "60",
            "@ClaimId": "1",
            "@PolicyId": "2",
            "@AgencyId": "1",
            "@ClaimNumber": "WC2019-EGN-001",
            "@PolicyNumber": "FAI100-1500-002",
            "@ClaimantName": "Alan Owens",
            "@MaritalStatus": "Married",
            "@JurisdictionState": "California"
          },
          {
            "@CompanyId": "70",
            "@ClaimId": "2",
            "@PolicyId": "4",
            "@AgencyId": "3",
            "@ClaimNumber": "WC2019-CCW-0401",
            "@PolicyNumber": "CIC115-0200-301",
            "@ClaimantName": "Maria Stevens",
            "@MaritalStatus": "Single",
            "@JurisdictionState": "Illinois"
          }
        ]
      }
    }
  }
}

Expected Result :-

{
  "?xml": {
    "version": "1.0",
    "encoding": "utf-8"
  },
  "Report": {    
    "Name": "<report-name>",
    "xmlns:xsi": "http://www.w3.org/2001/XMLSchema-instance",
    "xmlns": "<report-name>",
    "Tablix1": {
      "Details_Collection": {
        "Details": [
          {
            "CompanyId": "60",
            "ClaimId": "1",
            "PolicyId": "2",
            "AgencyId": "1",
            "ClaimNumber": "WC2019-EGN-001",
            "PolicyNumber": "FAI100-1500-002",
            "ClaimantName": "Alan Owens",
            "MaritalStatus": "Married",
            "JurisdictionState": "California"
          },
          {
            "CompanyId": "70",
            "ClaimId": "2",
            "PolicyId": "4",
            "AgencyId": "3",
            "ClaimNumber": "WC2019-CCW-0401",
            "PolicyNumber": "CIC115-0200-301",
            "ClaimantName": "Maria Stevens",
            "MaritalStatus": "Single",
            "JurisdictionState": "Illinois"
          }
        ]
      }
    }
  }
}

0 个答案:

没有答案