解析来自 Restful Web 服务的响应的 C# 代码



我有一个读取响应的网络服务。它无法分析响应数据,因为emsg为 null。

下面是连接到它的 restful Web 服务,以及返回的数据。

if (response.Body != null && response.Body != "null")
{
var xDocument = XDocument.Parse(response.Body);
var emsg = xDocument.Element("T_OUTPUT").LastNode;
if (emsg != null)
{
var parsedData =
(
from e in xDocument.Element("items").Descendants()
select new ProductPipelineTankInventoryEntity
{
BUKRS = (long)e.Element("BUKRS"),
WERKS = (long)e.Element("WERKS"),
NAME1 = e.Element("NAME1").ToString(),
REGIO = e.Element("REGIO").ToString(),
MATKL = e.Element("MATKL").ToString(),
MATNR = (long)e.Element("MATNR"),
LGORT = e.Element("LGORT").ToString(),
GRDIP = (double)e.Element("GRDIP"),
TRNDATE = DateTime.Parse(e.Element("TRNDATE").ToString()),
VOL_NATURAL = (double)e.Element("VOL_NATURAL"),
GRDIP_RUN = (double)e.Element("GRDIP_RUN"),
VOL_RUNNING = (double)e.Element("VOL_RUNNING")
}
).ToList();
ret.AddRange(parsedData);
}
}

<ns0:YV_PIPELINE_PLANT_DIP_DETAILS.Response xmlns:ns0="urn:sap-com:document:sap:rfc:functions">
<E_MSG/>
<T_OUTPUT>
<item>
<BUKRS>2200</BUKRS>
<WERKS>2222</WERKS>
<NAME1>IOCL,JHARSUGUDA TERMINAL</NAME1>
<REGIO>OR</REGIO>
<MATKL>BULK-MS</MATKL>
<MATNR>16400</MATNR>
<LGORT>T005</LGORT>
<GRDIP>1005.5</GRDIP>
<TRNDATE>2019-08-05</TRNDATE>
<VOL_NATURAL>2048.080</VOL_NATURAL>
<GRDIP_RUN>0825.5</GRDIP_RUN>
<VOL_RUNNING>1686.155</VOL_RUNNING>
</item>
<item>
<BUKRS>2200</BUKRS>
<WERKS>2222</WERKS>
<NAME1>IOCL,JHARSUGUDA TERMINAL</NAME1>
<REGIO>OR</REGIO>
<MATKL>BULK-MS</MATKL>
<MATNR>16400</MATNR>
<LGORT>T006</LGORT>
<GRDIP>173.9</GRDIP>
<TRNDATE>2019-08-05</TRNDATE>
<VOL_NATURAL>776.741</VOL_NATURAL>
<GRDIP_RUN>0915.9</GRDIP_RUN>
<VOL_RUNNING>4130.508</VOL_RUNNING>
</item>
</T_OUTPUT>
</ns0:YV_PIPELINE_PLANT_DIP_DETAILS.Response>

上面的代码我试过了。下面的部分是响应输出。

您需要具有命名空间并从"项目"更改为"项目"。 为了测试,我从文件中读取xml而不是响应。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
using System.Globalization;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
string xmlResponse = File.ReadAllText(FILENAME);
XDocument xDocument = XDocument.Parse(xmlResponse);
XElement emsg = xDocument.Descendants().Where(x => x.Name.LocalName == "T_OUTPUT").FirstOrDefault();
XNamespace ns = emsg.GetDefaultNamespace();
if (emsg != null)
{
var parsedData =
(
from e in xDocument.Descendants(ns + "item")
select new ProductPipelineTankInventoryEntity
{
BUKRS = (long)e.Element(ns + "BUKRS"),
WERKS = (long)e.Element(ns + "WERKS"),
NAME1 = (string)e.Element(ns + "NAME1"),
REGIO = (string)e.Element(ns + "REGIO"),
MATKL = (string)e.Element(ns + "MATKL"),
MATNR = (long)e.Element(ns + "MATNR"),
LGORT = (string)e.Element(ns + "LGORT"),
GRDIP = (double)e.Element(ns + "GRDIP"),
TRNDATE = (DateTime)e.Element(ns + "TRNDATE"),
VOL_NATURAL = (double)e.Element(ns + "VOL_NATURAL"),
GRDIP_RUN = (double)e.Element(ns + "GRDIP_RUN"),
VOL_RUNNING = (double)e.Element(ns + "VOL_RUNNING")
}
).ToList();
}
}
}
public class ProductPipelineTankInventoryEntity
{
public long BUKRS { get; set; }
public long WERKS { get; set; }
public string NAME1 { get; set; }
public string REGIO { get; set; }
public string MATKL { get; set; }
public long MATNR { get; set; }
public string LGORT { get; set; }
public double GRDIP { get; set; }
public DateTime TRNDATE { get; set; }
public double VOL_NATURAL { get; set; }
public double GRDIP_RUN { get; set; }
public double VOL_RUNNING { get; set; }
}

}

相关内容

  • 没有找到相关文章

最新更新