public static bool UserPropertiesToXml(UserProperties userProperties, out XmlDocument returnXmlDocument) { returnXmlDocument = new XmlDocument(); try { System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); XmlSerializer serializer = new XmlSerializer(typeof(UserProperties)); serializer.Serialize(memoryStream, userProperties); string xmlString = ASCIIEncoding.UTF8.GetString(memoryStream.ToArray()); //HACK: similiarly to creating an xml file via right clicking the project and making a new file, // when windows creates an xml file, it tags on a hidden character at the beginning of the file. we have seen this problem before // apparently this ALSO happens when creating an xml file with the XmlWriter.. which happens when we Serialize UserProperties to Xml // to prevent against this error when loading a string generated from an xml writer, we grab the substring of <UserProperties> ... </UserProperties> // this manages to successfully strip out the beginning junk character // we should eventually find a better way to do this.. but for now, it does the trick int indexOfUserPropertiesTag = xmlString.IndexOf("<" + ConstStrings.kUserProperties + ">"); xmlString = xmlString.Substring(indexOfUserPropertiesTag, xmlString.Length - indexOfUserPropertiesTag); returnXmlDocument.LoadXml(xmlString); return(true); } catch (System.Exception ex) { throw ex; } }
public static bool UserPropertiesFromXml(XmlNode xmlNode, out UserProperties userProperties) { userProperties = null; try { XmlReader xmlReader = new XmlNodeReader(xmlNode); XmlSerializer serializer = new XmlSerializer(typeof(UserProperties)); userProperties = (UserProperties)serializer.Deserialize(xmlReader); return(true); } catch (System.Exception ex) { throw ex; } }