static public string SerializeToXml<T>(T obj) { string xmlString = string.Empty; //XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); //using (MemoryStream ms = new MemoryStream()) //{ // xmlSerializer.Serialize(ms, obj); // xmlString = Encoding.UTF8.GetString(ms.ToArray()); //} Encoding encoding = Encoding.UTF8; using (MemoryStream memoryStream = new MemoryStream()) { XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType()); XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces(); namespaces.Add("", ""); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, encoding); xmlTextWriter.Formatting = Formatting.None; xmlSerializer.Serialize(xmlTextWriter, obj, namespaces); xmlTextWriter.Flush(); xmlTextWriter.Close(); xmlString = encoding.GetString(memoryStream.ToArray()); } return xmlString; } static public T DeserializeXml<T>(string xmlString) { T t = default(T); XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (Stream xmlStream = new MemoryStream(Encoding.UTF8.GetBytes(xmlString))) { using (XmlReader xmlReader = XmlReader.Create(xmlStream)) { Object obj = xmlSerializer.Deserialize(xmlReader); t = (T)obj; } } return t; } static public string VerifyXml(string xml) { string result = string.Empty; try { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); } catch (XmlException ex) { result = ex.Message; } return result; } static public string GetXmlNodeText(string xml, string nodeName) { string result = string.Empty; try { var doc = new XmlDocument(); doc.LoadXml(xml); XmlNode entityName = doc.SelectSingleNode(nodeName); if (entityName != null) { result = entityName.InnerText.Trim(); } } catch { } return result; }