using System; using System.Data; using System.Configuration; using System.Web; using System.Xml; using System.Collections; using System.Collections.Generic; using System.Collections.Specialized; using System.Xml.Serialization; using System.IO; using System.Text.RegularExpressions; namespace Pcb.Common { /// /// XmlHelper 的摘要说明 /// public class XmlHelper { public XmlHelper() { } /// /// 返回XmlDocument对象 /// /// 路径 /// public static XmlDocument GetXmlDoc(string path) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); return doc; } catch { return null; } } /// /// 读取数据 /// /// 路径 /// 节点 /// 属性名,非空时返回该属性值,否则返回串联值 /// string /************************************************** * 使用示列: * XmlHelper.Read(path, "/Node", "") * XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute") ************************************************/ public static string Read(string path, string node, string attribute) { string value = ""; try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value); } catch (Exception ee) { return ee.Message.ToString(); } return value; } public static string Read(XmlDocument doc, string node, string attribute) { string value = ""; try { XmlNode xn = doc.SelectSingleNode(node); value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value); } catch { } return value; } /// /// 读取节点 /// /// /// /// public static XmlNode Read(string path, string node) { XmlNode xn = null; try { XmlDocument doc = new XmlDocument(); doc.Load(HttpContext.Current.Server.MapPath("~")+path); xn = doc.SelectSingleNode(node); return xn; } catch { return null; } } /// /// 获取子集列表 /// /// /// /// public static List ReadChildList(string path, string node) { List list = new List(); try { XmlDocument doc = new XmlDocument(); doc.Load(AppDomain.CurrentDomain.BaseDirectory + path); XmlNode xn = doc.SelectSingleNode(node); foreach (XmlNode xn2 in xn.ChildNodes) { list.Add(xn2); } } catch(Exception ex) { LogHelper.Error(ex.ToString()); } return list; } public static XmlNodeList ReadXmlNodeList(string path, string node) { XmlNodeList list = null; try { XmlDocument doc = new XmlDocument(); doc.Load(path); return doc.SelectNodes(node); } catch { } return list; } public static XmlNodeList ReadXmlNodeList(XmlDocument doc, string node) { XmlNodeList list = null; try { return doc.SelectNodes(node); } catch { } return list; } /// /// 插入数据 /// /// 路径 /// 节点 /// 元素名,非空时插入新元素,否则在该元素中插入属性 /// 属性名,非空时插入该元素属性值,否则插入元素值 /// 值 /// /************************************************** * 使用示列: * XmlHelper.Insert(path, "/Node", "Element", "", "Value") * XmlHelper.Insert(path, "/Node", "Element", "Attribute", "Value") * XmlHelper.Insert(path, "/Node", "", "Attribute", "Value") ************************************************/ public static void Insert(string path, string node, string element, string attribute, string value) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); string[] attrs = { attribute }; string[] vals = { value }; Insert(doc, path, node, element, attrs, vals); } catch { } } public static void Insert(string path, string node, string element, string[] attribute, string[] value) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); Insert(doc, path, node, element, attribute, value); } catch { } } public static void Insert(XmlDocument doc, string path, string node, string element, string[] attributes, string[] values) { try { XmlNode xn = doc.SelectSingleNode(node); if (element.Equals("")) { if (attributes.Length > 0) { XmlElement xe = (XmlElement)xn; for (int i = 0; i < attributes.Length; i++) { xe.SetAttribute(attributes[i], values[i]); } } } else { XmlElement xe = doc.CreateElement(element); if (attributes.Length <= 0) xe.InnerText = values[0]; else { for (int i = 0; i < attributes.Length; i++) { xe.SetAttribute(attributes[i], values[i]); } } xn.AppendChild(xe); } doc.Save(path); } catch { } } /// /// 修改数据 /// /// 路径 /// 节点 /// 属性名,非空时修改该节点属性值,否则修改节点值 /// 值 /// /************************************************** * 使用示列: * XmlHelper.Insert(path, "/Node", "", "Value") * XmlHelper.Insert(path, "/Node", "Attribute", "Value") ************************************************/ public static void Update(string path, string node, string attribute, string value) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); doc.Save(path); } catch (Exception ee) { } } public static void Update(XmlDocument doc, string path, string node, string attribute, string value) { try { XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xe.InnerText = value; else xe.SetAttribute(attribute, value); doc.Save(path); } catch { } } /// /// 更新子节点 /// /// /// /// public static void UpdateXml(string path, string node, string xml) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; xe.InnerXml = xml; doc.Save(path); } catch { } } /// /// 删除数据 /// /// 路径 /// 节点 /// 属性名,非空时删除该节点属性值,否则删除节点值 /// 值 /// /************************************************** * 使用示列: * XmlHelper.Delete(path, "/Node", "") * XmlHelper.Delete(path, "/Node", "Attribute") ************************************************/ public static void Delete(string path, string node, string attribute) { try { XmlDocument doc = new XmlDocument(); doc.Load(path); XmlNode xn = doc.SelectSingleNode(node); XmlElement xe = (XmlElement)xn; if (attribute.Equals("")) xn.ParentNode.RemoveChild(xn); else xe.RemoveAttribute(attribute); doc.Save(path); } catch { } } public static XmlDocument GetRemoteXml(string URL) { //使用rssURL的值建立了一个WebRequest项 System.Net.WebRequest myRequest = System.Net.WebRequest.Create(URL); //WebRequest请求的响应将会被放到一个WebResponse对象myResponse里,然后这个WebResponse对象被用来建立一个流来取出XML的值 System.Net.WebResponse myResponse = myRequest.GetResponse(); System.IO.Stream stream = myResponse.GetResponseStream(); //使用一个XmlDocument对象rssDoc来存储流中的XML内容。XmlDocument对象用来调入XML的内容 System.Xml.XmlDocument doc = new System.Xml.XmlDocument(); doc.Load(stream); return doc; } public static bool SuccessResponse(string xml, out string msg) { try { if (xml.Trim().Length == 0) { msg = string.Empty; return false; } XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); int code = GetXmlAttr(doc, "//Response", "code"); msg = GetXmlAttr(doc, "//Response", "msg"); return code == 200; } catch (Exception ex) { msg = "SuccessResponse:" + ex.Message; return false; } } public static bool SuccessResponse(string xml) { try { if (xml.Trim().Length == 0) { return false; } XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); int code = GetXmlAttr(doc, "//Response", "code"); return code == 200; } catch (Exception) { return false; } } /// /// 对象序列化成 XML String /// public static string XmlSerialize(T obj) { if (obj == null) { return null; } XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ////Add an empty namespace and empty value ns.Add("", ""); MemoryStream Stream = new MemoryStream(); XmlSerializer xml = new XmlSerializer(typeof(T)); try { //序列化对象 xml.Serialize(Stream, obj, ns); } catch (InvalidOperationException) { return null; } Stream.Position = 0; StreamReader sr = new StreamReader(Stream); string str = sr.ReadToEnd(); sr.Dispose(); Stream.Dispose(); Regex reg2 = new Regex(@"<[?]xml[\w\W]+?[?]>"); str = reg2.Replace(str, string.Empty).Trim(); return str; } /// /// XML String 反序列化成对象 /// public static T XmlDeserialize(string xmlString) { try { using (StringReader sr = new StringReader(xmlString)) { XmlSerializer xmldes = new XmlSerializer(typeof(T)); return (T)xmldes.Deserialize(sr); } } catch (Exception e) { return default(T); } } public static T GetXmlAttr(string xml, string path, string attrName) { if (xml.Trim().Length == 0) { return default(T); } XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); if (doc == null) { return default(T); } var node = doc.SelectSingleNode(path); if (node == null) { return default(T); } if (node.Attributes[attrName] == null) { return default(T); } return (T)Convert.ChangeType(node.Attributes[attrName].Value, typeof(T)); } public static T GetXmlAttr(XmlDocument doc, string path, string attrName) { if (doc == null) { return default(T); } var node = doc.SelectSingleNode(path); if (node == null) { return default(T); } if (node.Attributes[attrName] == null) { return default(T); } return (T)Convert.ChangeType(node.Attributes[attrName].Value, typeof(T)); } public static string GetXml(XmlDocument doc, string path) { if (doc == null) { return string.Empty; } var node = doc.SelectSingleNode(path); if (node == null) { return string.Empty; } return node.InnerXml; } public static T GetValue(XmlDocument doc, string path) { if (doc == null) { return default(T); } var node = doc.SelectSingleNode(path); if (node == null) { return default(T); } return (T)Convert.ChangeType(node.InnerText, typeof(T)); } XmlDocument doc = null; XmlElement responseNode = null; XmlElement resultStart = null; public XmlHelper(int code) { doc = new XmlDocument(); responseNode = doc.CreateElement("Response"); responseNode.SetAttribute("code", code.ToString()); } public void Add(string name, object value) { XmlElement el = doc.CreateElement(name); el.InnerText = Convert.ToString(value).Trim(); resultStart.AppendChild(el); } public void AddResultTagBegin(string name) { resultStart = doc.CreateElement(name); } public void AddResultTagBegin() { AddResultTagBegin("Result"); } public void AddResultTagEnd() { responseNode.AppendChild(resultStart); } public override string ToString() { doc.AppendChild(responseNode); string xml = doc.OuterXml.ToString(); return xml; } } }