/// <summary> /// Deserializes XML string into object. /// </summary> /// <param name="source">XML string.</param> /// <param name="typeInfo">Object type info.</param> /// <returns>Deserialized object.</returns> public static object DeserializeObjectFromXml(string source, Type typeInfo) { if (string.IsNullOrEmpty(source)) { return(null); } XmlSerializer serializer = new XmlSerializer(typeInfo); StringReader sourceReader = null; System.Xml.XmlReader reader = null; try { sourceReader = new StringReader(source); XmlReaderSettings settings = new XmlReaderSettings(); settings.XmlResolver = null; reader = System.Xml.XmlReader.Create(sourceReader, settings); sourceReader = null; return(serializer.Deserialize(reader)); } finally { if (reader != null) { reader.Dispose(); } if (sourceReader != null) { sourceReader.Dispose(); } } }
/// <summary> /// Deserializes the customer order information from the specified XML blob. /// </summary> /// <param name="orderXml">The order XML blob.</param> /// <returns>The customer order information.</returns> public static CustomerOrderInfo FromXml(string orderXml) { CustomerOrderInfo orderInfo; XmlSerializer serializer = new XmlSerializer(typeof(CustomerOrderInfo)); StringReader orderXmlReader = null; System.Xml.XmlReader reader = null; try { orderXmlReader = new StringReader(orderXml); XmlReaderSettings settings = new XmlReaderSettings(); settings.XmlResolver = null; reader = System.Xml.XmlReader.Create(orderXmlReader, settings); orderXmlReader = null; serializer = new XmlSerializer(typeof(CustomerOrderInfo)); orderInfo = (CustomerOrderInfo)serializer.Deserialize(reader); } finally { if (reader != null) { reader.Dispose(); } if (orderXmlReader != null) { orderXmlReader.Dispose(); } } return(orderInfo); }
/// <summary> /// Check whether or not the .xml file starts with a node. If not label it as empty. /// </summary> private async Task <bool> checkFileEmpty() { System.Xml.XmlReader XmlReader = System.Xml.XmlReader.Create(await HighscoreIO.HighscoreFile.OpenStreamForReadAsync()); try { XmlReader.Read(); } catch (XmlException e) { if (e.Message.StartsWith("Root element")) { XmlReader.Dispose(); return(true); } else { System.Diagnostics.Debugger.Break(); } } XmlReader.Dispose(); return(false); }
public void InitReaderPointer(XmlReader DataReader) { if (this.Desc == "BeforeRead") { IntegrityVer = EINTEGRITY.BEFORE_READ; TestLog.Compare(DataReader.ReadState, ReadState.Initial, "ReadState=Initial"); TestLog.Compare(DataReader.EOF, false, "EOF==false"); } else if (this.Desc == "AfterReadIsFalse") { IntegrityVer = EINTEGRITY.AFTER_READ_FALSE; while (DataReader.Read()) ; TestLog.Compare(DataReader.ReadState, ReadState.EndOfFile, "ReadState=EOF"); TestLog.Compare(DataReader.EOF, true, "EOF==true"); } else if (this.Desc == "AfterClose") { IntegrityVer = EINTEGRITY.AFTER_CLOSE; while (DataReader.Read()) ; DataReader.Dispose(); TestLog.Compare(DataReader.ReadState, ReadState.Closed, "ReadState=Closed"); TestLog.Compare(DataReader.EOF, false, "EOF==true"); } else if (this.Desc == "AfterCloseInTheMiddle") { IntegrityVer = EINTEGRITY.CLOSE_IN_THE_MIDDLE; for (int i = 0; i < 1; i++) { if (false == DataReader.Read()) throw new TestFailedException(""); TestLog.Compare(DataReader.ReadState, ReadState.Interactive, "ReadState=Interactive"); } DataReader.Dispose(); TestLog.Compare(DataReader.ReadState, ReadState.Closed, "ReadState=Closed"); TestLog.Compare(DataReader.EOF, false, "EOF==true"); } else if (this.Desc == "AfterResetState") { IntegrityVer = EINTEGRITY.AFTER_RESETSTATE; // position the reader somewhere in the middle of the file PositionOnElement(DataReader, "elem1"); TestLog.Compare(DataReader.ReadState, ReadState.Initial, "ReadState=Initial"); } }
public void Dispose() { _reader.Dispose(); _context.Dispose(); }
/// <summary> /// Loads the objects specified in the given XML container. /// </summary> /// <param name="xmlReader">The XML reader.</param> /// <returns>The instances created from the XML definition.</returns> /// <exception cref="ArgumentNullException"> /// <paramref name="xmlReader"/> is <see langword="null"/>. /// </exception> /// <exception cref="SerializationException"> /// Error while loading the objects. See exception message. /// </exception> public IEnumerable Load(XmlReader xmlReader) { if (xmlReader == null) throw new ArgumentNullException("xmlReader"); // Get root node. if (!xmlReader.ReadToFollowing("Layout")) throw new SerializationException("Could not find XML node 'Layout'."); xmlReader = xmlReader.ReadSubtree(); xmlReader.Read(); // Get default namespace from attribute. DefaultNamespace = xmlReader.GetAttribute("DefaultNamespace"); // Load all objects and add to list. var objects = new List<object>(); while (xmlReader.Read()) { if (xmlReader.NodeType != XmlNodeType.Element) continue; var innerReader = xmlReader.ReadSubtree(); innerReader.Read(); objects.Add(CreateInstance(innerReader)); #if PORTABLE || WINDOWS_UWP innerReader.Dispose(); #else innerReader.Close(); #endif } #if PORTABLE || WINDOWS_UWP xmlReader.Dispose(); #else xmlReader.Close(); #endif DefaultNamespace = null; return objects; }