我有一个读取响应的Web服务。由于emsg
为空,因此无法解析响应数据。
下面是连接的静态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>
我尝试过的上面的代码。下面是响应输出。
答案 0 :(得分:0)
您需要具有名称空间,并从“项目”更改为“项目”。为了进行测试,我从文件而不是响应中读取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; }
}
}