老阴阳人了

导航

xml解析方法

 

/// 通过实例返回 XML 字符串
/// <summary>
/// 通过实例返回 XML 字符串
/// 例:str = OpConvertXml《OutXml》.GetXmlString(oXml);
/// </summary>
/// <param name="t">t的实例</param>
/// <returns></returns>
public static ReturnMsg<string> GetXmlString(T t)
{
//return GetXmlString(t, Encoding.GetEncoding("GBK"));
return GetXmlStringStream(t, Encoding.UTF8);
}

 

/// 通过实例返回 XML 字符串
/// <summary>
/// 通过实例返回 XML 字符串
/// 例:str = OpConvertXml《OutXml》.GetXmlString(oXml);
/// </summary>
/// <param name="t">t的实例</param>
/// <param name="encode">返回的xml串字符编码类型</param>
/// <returns></returns>
public static ReturnMsg<string> GetXmlStringStream(T t, Encoding encode)
{
StreamWriter myWriter = null;
XmlSerializer mySerializer = null;
ReturnMsg<string> rtm = new ReturnMsg<string>();
try
{
using (MemoryStream stream = new MemoryStream()) //convert stream 2 string
{
mySerializer = new XmlSerializer(typeof(T));

myWriter = new StreamWriter(stream, encode, 102400);
mySerializer.Serialize(myWriter, t);
myWriter.Flush();
rtm.clsObj = encode.GetString(stream.ToArray());
myWriter.Close();
myWriter.Dispose();
mySerializer = null;
if (encode == Encoding.UTF8)
{
rtm.clsObj = rtm.clsObj.Substring(1);
}
rtm.IsSuccess = true;
}
}
catch (Exception ex)
{
myWriter = null;
mySerializer = null;
rtm.IsSuccess = false;
rtm.SInnerMsg = ex.Message + "\n Source:" + ex.Source;
rtm.SMsg = ex.Message;
}
return rtm;
}

 

//JzyyYyhsYysqOut 实体类

//ress2入参的xml

//调用解析

public class  xml

{

   HttpWebProxy.ReturnMsg<string> rtnMOut2 = HttpWebProxy.OpCvtXmlNew<Response<JzyyYyhsYysqOut>>.GetXmlString(ress2);

}

posted on 2020-08-12 16:55  老阴阳人了  阅读(74)  评论(0编辑  收藏  举报