其他分享
首页 > 其他分享> > 记:.net读取海关金二客户端回执报文

记:.net读取海关金二客户端回执报文

作者:互联网

金二客户端回执报文有BUG,数组与对象不定回传,读取问题,按照节点读取报文内容,反序列化简单实现,不能通用,需要判断节点。

using (FileStream fsRead = file.OpenRead())
{
string xmltext = string.Empty;
int fsLen = (int)fsRead.Length;
byte[] heByte = new byte[fsLen];
int r = fsRead.Read(heByte, 0, heByte.Length);
xmltext = System.Text.Encoding.UTF8.GetString(heByte);

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmltext);//加载xml

if (doc.GetElementsByTagName("Package").Count < 1)
return null;

XmlElement package = (XmlElement)doc.GetElementsByTagName("Package")[0];

if (package.GetElementsByTagName("EnvelopInfo").Count < 1)
return null;

XmlElement envelopInfo = (XmlElement)package.GetElementsByTagName("EnvelopInfo")[0];

nodeType = (envelopInfo["message_type"]).InnerText;

if (package.GetElementsByTagName("DataInfo").Count < 1)
return null;

XmlElement dataInfo = (XmlElement)package.GetElementsByTagName("DataInfo")[0];

if (dataInfo.GetElementsByTagName("BussinessData").Count < 1)
return null;

XmlElement bussinessData = (XmlElement)dataInfo.GetElementsByTagName("BussinessData")[0];

if (bussinessData.GetElementsByTagName(nodeType).Count < 1)
return null;

string text = JsonConvert.SerializeXmlNode(bussinessData.GetElementsByTagName(nodeType)[0]);

JObject jObject = JObject.Parse(text);

return jObject[nodeType].ToString();
}

标签:Count,return,package,GetElementsByTagName,XmlElement,net,null,金二,回执
来源: https://www.cnblogs.com/yangruibokeyuan/p/16374852.html