一、实体类转为 XML字符串
string requestXML = Serializer(typeof(CnoocSAPAccountPay.DT_Entrade_AccountPay_SAP_Request), req);
private string Serializer(Type type, object obj)
{
MemoryStream Stream = new MemoryStream();
XmlSerializer xml = new XmlSerializer(type);
try
{
xml.Serialize(Stream, obj);
}
catch (InvalidOperationException)
{
throw;
}
Stream.Position = 0;
StreamReader sr = new StreamReader(Stream);
string str = sr.ReadToEnd();
sr.Dispose();
Stream.Dispose();
return str;
}
二、string 转 xml
string content = Serializer(req.GetType(), req);
XmlDocument xdocC = new XmlDocument();
xdocC.LoadXml(content);
XmlNodeList topMC = xdocC.SelectNodes("//DT_Entrade_AccountPay_SAP_Request");
foreach (XmlElement tm in topMC)
{
content = tm.InnerXml.ToString();
}
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(postResult);
XmlNodeList topR = xdoc.SelectNodes("//Return");
XmlNodeList topI = xdoc.SelectNodes("//Item");
foreach (XmlElement tm in topR)
{
result.RetCode = tm.GetElementsByTagName("RetCode")[0].InnerText;
result.RetMsg = tm.GetElementsByTagName("RetMsg")[0].InnerText;
}