解析来自 Restful Web 服务的响应的 C# 代码
Posted
技术标签:
【中文标题】解析来自 Restful Web 服务的响应的 C# 代码【英文标题】:C# Code that Parses the Response from Restful Webservice 【发布时间】:2019-08-11 16:13:41 【问题描述】:我有一个读取响应的网络服务。它无法解析响应数据,因为emsg
为空。
下面是restful webservice,连接到它,返回的数据。
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>
上面的代码我试过了。以下部分是响应输出。
【问题讨论】:
response.Body 是从哪里来的?显示相关代码并清楚涉及的类。 “我无法解析响应”:总是发布完整的错误消息、意外结果等。xDocument.Element("items")
在您提供的示例中不存在,因此一旦您到达该点,这将是一个问题。
大量编辑了你的问题,我意识到你的英语不好。我希望这就是你的意思。它应该可以帮助您及时得到答案。
【参考方案1】:
您需要有命名空间并将“item”更改为“item”。为了测试,我从文件而不是响应中读取 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:\temp\test.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;
【讨论】:
以上是关于解析来自 Restful Web 服务的响应的 C# 代码的主要内容,如果未能解决你的问题,请参考以下文章