public static T?DataDeserialize <T>(TextReader input, XmlReaderSettings?settings = null, IEnumerable <Type>?knownTypes = null) { DataContractSerializer serializer = new DataContractSerializer(typeof(T), knownTypes); using XmlReader reader = XmlReader.Create(input, settings); return((T?)serializer.ReadObject(reader)); }
public static XmlReader CreateReader(XmlReader baseReader, WhitespaceRuleLookup?wsRules) { if (wsRules == null) { return(baseReader); // There is no rules to process } XmlReaderSettings?readerSettings = baseReader.Settings; if (readerSettings != null) { if (readerSettings.IgnoreWhitespace) { return(baseReader); // V2 XmlReader that strips all WS } } else { XmlTextReader?txtReader = baseReader as XmlTextReader; if (txtReader != null && txtReader.WhitespaceHandling == WhitespaceHandling.None) { return(baseReader); // V1 XmlTextReader that strips all WS } XmlTextReaderImpl?txtReaderImpl = baseReader as XmlTextReaderImpl; if (txtReaderImpl != null && txtReaderImpl.WhitespaceHandling == WhitespaceHandling.None) { return(baseReader); // XmlTextReaderImpl that strips all WS } } return(new WhitespaceRuleReader(baseReader, wsRules)); }
/// <summary> /// Returns an XmlReader that decrypts data transparently. /// </summary> public XmlReader CreateDecryptingXmlReader(Stream input, XmlReaderSettings?settings) { // XML-based configurations aren't really all that big, so we can buffer // the whole thing in memory while we determine decryption operations. var memStream = new MemoryStream(); input.CopyTo(memStream); memStream.Position = 0; // First, consume the entire XmlReader as an XmlDocument. var document = new XmlDocument(); using (var reader = XmlReader.Create(memStream, settings)) { document.Load(reader); } memStream.Position = 0; if (ContainsEncryptedData(document)) { // DecryptDocumentAndCreateXmlReader is not supported on 'browser', // but we only call it depending on the input XML document. If the document // is encrypted and this is running on 'browser', just let the PNSE throw. #pragma warning disable CA1416 return(DecryptDocumentAndCreateXmlReader(document)); #pragma warning restore CA1416 } else { // If no decryption would have taken place, return a new fresh reader // based on the memory stream (which doesn't need to be disposed). return(XmlReader.Create(memStream, settings)); } }
public QueryReaderSettings(XmlNameTable xmlNameTable) { Debug.Assert(xmlNameTable != null); _xmlReaderSettings = new XmlReaderSettings(); _xmlReaderSettings.NameTable = xmlNameTable; _xmlReaderSettings.ConformanceLevel = ConformanceLevel.Document; _xmlReaderSettings.XmlResolver = null; _xmlReaderSettings.DtdProcessing = DtdProcessing.Prohibit; _xmlReaderSettings.CloseInput = true; }
public static T?DataDeserialize <T>(FileInfo file, XmlReaderSettings?settings = null, IEnumerable <Type>?knownTypes = null) { if (file == null) { throw new ArgumentNullException(nameof(file)); } using FileStream fs = File.OpenRead(file.FullName); return(DataDeserialize <T>(fs, settings, knownTypes)); }
public QueryReaderSettings(XmlReader reader) { #pragma warning disable 618 XmlValidatingReader?valReader = reader as XmlValidatingReader; #pragma warning restore 618 if (valReader != null) { // Unwrap validation reader _validatingReader = true; reader = valReader.Impl.Reader; } _xmlReaderSettings = reader.Settings; if (_xmlReaderSettings != null) { _xmlReaderSettings = _xmlReaderSettings.Clone(); _xmlReaderSettings.NameTable = reader.NameTable; _xmlReaderSettings.CloseInput = true; _xmlReaderSettings.LineNumberOffset = 0; _xmlReaderSettings.LinePositionOffset = 0; XmlTextReaderImpl?impl = reader as XmlTextReaderImpl; if (impl != null) { _xmlReaderSettings.XmlResolver = impl.GetResolver(); } } else { _xmlNameTable = reader.NameTable; XmlTextReader?xmlTextReader = reader as XmlTextReader; if (xmlTextReader != null) { XmlTextReaderImpl impl = xmlTextReader.Impl; _entityHandling = impl.EntityHandling; _namespaces = impl.Namespaces; _normalization = impl.Normalization; _prohibitDtd = (impl.DtdProcessing == DtdProcessing.Prohibit); _whitespaceHandling = impl.WhitespaceHandling; _xmlResolver = impl.GetResolver(); } else { _entityHandling = EntityHandling.ExpandEntities; _namespaces = true; _normalization = true; _prohibitDtd = true; _whitespaceHandling = WhitespaceHandling.All; _xmlResolver = null; } } }
/// <summary> /// 将xml文本反序列化对象 /// </summary> /// <param name="xml">xml文本内容</param> /// <param name="objType">对象类型</param> /// <param name="options">配置选项</param> /// <returns></returns> public static object?Deserialize(string?xml, Type objType, XmlReaderSettings?options) { if (objType == null) { throw new ArgumentNullException(nameof(objType)); } if (string.IsNullOrEmpty(xml)) { return(objType.DefaultValue()); } var settings = options ?? readerSettings; using var reader = new StringReader(xml); using var xmlReader = XmlReader.Create(reader, settings); var xmlSerializer = new System.Xml.Serialization.XmlSerializer(objType); return(xmlSerializer.Deserialize(xmlReader)); }
internal XmlReader AddConformanceWrapper(XmlReader baseReader) { XmlReaderSettings?baseReaderSettings = baseReader.Settings; bool checkChars = false; bool noWhitespace = false; bool noComments = false; bool noPIs = false; DtdProcessing dtdProc = (DtdProcessing)(-1); bool needWrap = false; if (baseReaderSettings == null) { #pragma warning disable 618 if (_conformanceLevel != ConformanceLevel.Auto && _conformanceLevel != XmlReader.GetV1ConformanceLevel(baseReader)) { throw new InvalidOperationException(SR.Format(SR.Xml_IncompatibleConformanceLevel, _conformanceLevel.ToString())); } // get the V1 XmlTextReader ref XmlTextReader?v1XmlTextReader = baseReader as XmlTextReader; if (v1XmlTextReader == null) { XmlValidatingReader?vr = baseReader as XmlValidatingReader; if (vr != null) { v1XmlTextReader = (XmlTextReader)vr.Reader; } } // assume the V1 readers already do all conformance checking; // wrap only if IgnoreWhitespace, IgnoreComments, IgnoreProcessingInstructions or ProhibitDtd is true; if (_ignoreWhitespace) { WhitespaceHandling wh = WhitespaceHandling.All; // special-case our V1 readers to see if whey already filter whitespace if (v1XmlTextReader != null) { wh = v1XmlTextReader.WhitespaceHandling; } if (wh == WhitespaceHandling.All) { noWhitespace = true; needWrap = true; } } if (_ignoreComments) { noComments = true; needWrap = true; } if (_ignorePIs) { noPIs = true; needWrap = true; } // DTD processing DtdProcessing baseDtdProcessing = DtdProcessing.Parse; if (v1XmlTextReader != null) { baseDtdProcessing = v1XmlTextReader.DtdProcessing; } if ((_dtdProcessing == DtdProcessing.Prohibit && baseDtdProcessing != DtdProcessing.Prohibit) || (_dtdProcessing == DtdProcessing.Ignore && baseDtdProcessing == DtdProcessing.Parse)) { dtdProc = _dtdProcessing; needWrap = true; } #pragma warning restore 618 } else { if (_conformanceLevel != baseReaderSettings.ConformanceLevel && _conformanceLevel != ConformanceLevel.Auto) { throw new InvalidOperationException(SR.Format(SR.Xml_IncompatibleConformanceLevel, _conformanceLevel.ToString())); } if (_checkCharacters && !baseReaderSettings.CheckCharacters) { checkChars = true; needWrap = true; } if (_ignoreWhitespace && !baseReaderSettings.IgnoreWhitespace) { noWhitespace = true; needWrap = true; } if (_ignoreComments && !baseReaderSettings.IgnoreComments) { noComments = true; needWrap = true; } if (_ignorePIs && !baseReaderSettings.IgnoreProcessingInstructions) { noPIs = true; needWrap = true; } if ((_dtdProcessing == DtdProcessing.Prohibit && baseReaderSettings.DtdProcessing != DtdProcessing.Prohibit) || (_dtdProcessing == DtdProcessing.Ignore && baseReaderSettings.DtdProcessing == DtdProcessing.Parse)) { dtdProc = _dtdProcessing; needWrap = true; } } if (needWrap) { IXmlNamespaceResolver?readerAsNSResolver = baseReader as IXmlNamespaceResolver; if (readerAsNSResolver != null) { return(new XmlCharCheckingReaderWithNS(baseReader, readerAsNSResolver, checkChars, noWhitespace, noComments, noPIs, dtdProc)); } else { return(new XmlCharCheckingReader(baseReader, checkChars, noWhitespace, noComments, noPIs, dtdProc)); } } else { return(baseReader); } }
public static T?DataDeserialize <T>(string data, XmlReaderSettings?settings = null, IEnumerable <Type>?knownTypes = null) { byte[] bytes = Encoding.UTF8.GetBytes(data); using MemoryStream mem = new MemoryStream(bytes); return(DataDeserialize <T>(mem, settings, knownTypes)); }