using System; using System.ComponentModel; using System.IO; using System.Linq; using System.Xml; using System.Xml.Linq; using System.Xml.Serialization; namespace JiShe.CollectBus.Common.Extensions { public static class XmlExtensions { /// /// Froms the XML. /// /// /// The XML. /// [Description("将XML转换为Object")] public static object FromXml(this string xml) where T : new() { using (var sr = new StringReader(xml)) { var xmlSerializer = new XmlSerializer(typeof(T)); return (T)xmlSerializer.Deserialize(sr); } } /// /// Deserializes the specified XML document. /// /// /// The XML document. /// [Description("反序列化指定的XML文档")] public static T Deserialize(this XDocument xmlDocument) { var xmlSerializer = new XmlSerializer(typeof(T)); using (var reader = xmlDocument.CreateReader()) return (T)xmlSerializer.Deserialize(reader); } /// /// Gets an attribute's value from an Xml node. /// /// The Xml node /// Attribute name /// Value of the attribute [Description("从Xml节点获取属性的值")] public static string GetAttributeValueOrNull(this XmlNode node, string attributeName) { if (node.Attributes == null || node.Attributes.Count <= 0) { throw new Exception(node.Name + " node has not " + attributeName + " attribute"); } return node.Attributes .Cast() .Where(attr => attr.Name == attributeName) .Select(attr => attr.Value) .FirstOrDefault(); } } }