public class XMLHelper { #region 反序列化 /// <summary> /// 反序列化 /// </summary> /// <param name="type">类型</param> /// <param name="xml">XML字符串</param> /// <returns></returns> public static object Deserialize(Type type, string xml) { try { using (StringReader sr = new StringReader(xml)) { XmlSerializer xmldes = new XmlSerializer(type); return xmldes.Deserialize(sr); } } catch (Exception e) { return null; } } /// <summary> /// 反序列化 /// </summary> /// <param name="type"></param> /// <param name="xml"></param> /// <returns></returns> public static object Deserialize(Type type, Stream stream) { XmlSerializer xmldes = new XmlSerializer(type); return xmldes.Deserialize(stream); } #endregion #region 序列化 /// <summary> /// 序列化 /// </summary> /// <param name="type">类型</param> /// <param name="obj">对象</param> /// <returns></returns> public static 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; } #endregion /// <summary> /// 读取xml格式的字符串的指定节点值 /// </summary> /// <param name="xml">xml字符串</param> /// <param name="ElementNames">节点数组,由根节点到指定节点的节点名称</param> /// <returns></returns> public static string GetElement(string xml, string[] ElementNames) { //调用示例: /* <?xml version="1.0" encoding="utf-8"?> <YG_HCCGFHXX> <XMLDATA> <HEAD> <JSSJ>接收时间</JSSJ> <ZTCLJG>消息主体处理结果</ZTCLJG> <CWXX>错误提示内容</CWXX> <BZXX>备注信息</BZXX> </HEAD> <MAIN> <DDBH>采购单号</DDBH> </MAIN> <DETAIL> <STRUCT> <DDMXBH>采购单明细编号</DDMXBH> <SXH>顺序号</SXH> <HCTBDM>耗材统编代码</HCTBDM> <HCXFDM>耗材细分代码</HCXFDM> <YYBDDM>医院本地代码</YYBDDM> <QYKC>企业库存</QYKC> <CLJG>处理结果</CLJG> <CLQKMS>处理情况描述</CLQKMS> </STRUCT> </DETAIL> </XMLDATA> </YG_HCCGFHXX> * 如获取以上节点的采购单号值,xmlarr中不算根节点 * * string[] xmlarr = { "XMLDATA", "MAIN", "DDBH" }; string DDBH = XMLHelper.GetElement(xml, xmlarr); */ XElement root = XElement.Parse(xml.Trim()); for (int i = 0; i < ElementNames.Length; i++) { root = root.Element(ElementNames[i]); } return root.Value; } #region XML转义字符处理 /// <summary> /// XML转义字符处理 /// </summary> public static string ConvertXml(string xml) { xml = (char)1 + xml; //为了避免首字母为要替换的字符,前加前缀 for (int intNext = 0; true; ) { int intIndexOf = xml.IndexOf("&", intNext); intNext = intIndexOf + 1; //避免&被重复替换 if (intIndexOf <= 0) { break; } else { xml = xml.Substring(0, intIndexOf) + "&" + xml.Substring(intIndexOf + 1); } } for (; true; ) { int intIndexOf = xml.IndexOf("<"); if (intIndexOf <= 0) { break; } else { xml = xml.Substring(0, intIndexOf) + "<" + xml.Substring(intIndexOf + 1); } } for (; true; ) { int intIndexOf = xml.IndexOf(">"); if (intIndexOf <= 0) { break; } else { xml = xml.Substring(0, intIndexOf) + ">" + xml.Substring(intIndexOf + 1); } } for (; true; ) { int intIndexOf = xml.IndexOf("\""); if (intIndexOf <= 0) { break; } else { xml = xml.Substring(0, intIndexOf) + """ + xml.Substring(intIndexOf + 1); } } return xml.Replace(((char)1).ToString(), ""); } #endregion }
时间: 2024-10-13 06:36:21