コード例 #1
0
        private void LoadSchemaFromLocation(string uri)
        {
            // is x-schema
            if (!XdrBuilder.IsXdrSchema(uri))
            {
                return;
            }
            string     url       = uri.Substring(x_schema.Length);
            XmlReader? reader    = null;
            SchemaInfo?xdrSchema = null;

            try
            {
                Uri    ruri = this.XmlResolver !.ResolveUri(BaseUri, url);
                Stream stm  = (Stream)this.XmlResolver.GetEntity(ruri, null, null) !;
                reader = new XmlTextReader(ruri.ToString(), stm, NameTable);
                ((XmlTextReader)reader).XmlResolver = this.XmlResolver;
                Parser parser = new Parser(SchemaType.XDR, NameTable, SchemaNames, EventHandler);
                parser.XmlResolver = this.XmlResolver;
                parser.Parse(reader, uri);
                while (reader.Read())
                {
                    ;                   // wellformness check
                }
                xdrSchema = parser.XdrSchema;
            }
            catch (XmlSchemaException e)
            {
                SendValidationEvent(SR.Sch_CannotLoadSchema, new string[] { uri, e.Message }, XmlSeverityType.Error);
            }
            catch (Exception e)
            {
                SendValidationEvent(SR.Sch_CannotLoadSchema, new string[] { uri, e.Message }, XmlSeverityType.Warning);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
            }
            if (xdrSchema != null && xdrSchema.ErrorCount == 0)
            {
                schemaInfo !.Add(xdrSchema, EventHandler);
                SchemaCollection !.Add(uri, xdrSchema, null, false);
            }
        }
コード例 #2
0
        public void StartParsing(XmlReader reader, string?targetNamespace)
        {
            _reader           = reader;
            _positionInfo     = PositionInfo.GetPositionInfo(reader);
            _namespaceManager = reader.NamespaceManager;
            if (_namespaceManager == null)
            {
                _namespaceManager    = new XmlNamespaceManager(_nameTable);
                _isProcessNamespaces = true;
            }
            else
            {
                _isProcessNamespaces = false;
            }
            while (reader.NodeType != XmlNodeType.Element && reader.Read())
            {
            }

            _markupDepth    = int.MaxValue;
            _schemaXmlDepth = reader.Depth;
            SchemaType rootType = _schemaNames.SchemaTypeFromRoot(reader.LocalName, reader.NamespaceURI);

            if (!CheckSchemaRoot(rootType, out string?code))
            {
                throw new XmlSchemaException(code, reader.BaseURI, _positionInfo.LineNumber, _positionInfo.LinePosition);
            }

            if (_schemaType == SchemaType.XSD)
            {
                _schema         = new XmlSchema();
                _schema.BaseUri = new Uri(reader.BaseURI !, UriKind.RelativeOrAbsolute);
                _builder        = new XsdBuilder(reader, _namespaceManager, _schema, _nameTable, _schemaNames, _eventHandler);
            }
            else
            {
                Debug.Assert(_schemaType == SchemaType.XDR);
                _xdrSchema            = new SchemaInfo();
                _xdrSchema.SchemaType = SchemaType.XDR;
                _builder = new XdrBuilder(reader, _namespaceManager, _xdrSchema, targetNamespace, _nameTable, _schemaNames, _eventHandler);
                ((XdrBuilder)_builder).XmlResolver = _xmlResolver;
            }
        }
コード例 #3
0
ファイル: XmlLoader.cs プロジェクト: ThE-TiGeR/DotnetRuntime
        internal XmlNamespaceManager ParsePartialContent(XmlNode parentNode, string innerxmltext, XmlNodeType nt)
        {
            //the function shouldn't be used to set innerxml for XmlDocument node
            Debug.Assert(parentNode.NodeType != XmlNodeType.Document);
            _doc = parentNode.OwnerDocument;
            Debug.Assert(_doc != null);
            XmlParserContext pc = GetContext(parentNode);

            _reader = CreateInnerXmlReader(innerxmltext, nt, pc, _doc);
            try
            {
                _preserveWhitespace = true;
                bool bOrigLoading = _doc.IsLoading;
                _doc.IsLoading = true;

                if (nt == XmlNodeType.Entity)
                {
                    XmlNode?node = null;
                    while (_reader.Read() && (node = LoadNodeDirect()) != null)
                    {
                        parentNode.AppendChildForLoad(node, _doc);
                    }
                }
                else
                {
                    XmlNode?node = null;
                    while (_reader.Read() && (node = LoadNode(true)) != null)
                    {
                        parentNode.AppendChildForLoad(node, _doc);
                    }
                }

                _doc.IsLoading = bOrigLoading;
            }
            finally
            {
                _reader.Close();
            }

            return(pc.NamespaceManager !);
        }
コード例 #4
0
        public static State LoadFromFile(string path)
        {
            if (File.Exists(path))
            {
                try
                {
                    XmlSerializer?serializer = new XmlSerializer(typeof(State));
                    using (XmlReader? reader = XmlReader.Create(path))
                    {
                        return((State)serializer.Deserialize(reader));
                    }
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex, "Could not load app state file {0}", path);
                    return(new State());
                }
            }

            return(new State());
        }
コード例 #5
0
ファイル: XmlLoader.cs プロジェクト: ThE-TiGeR/DotnetRuntime
 internal void Load(XmlDocument doc, XmlReader reader, bool preserveWhitespace)
 {
     _doc = doc;
     // perf: unwrap XmlTextReader if no one derived from it
     if (reader.GetType() == typeof(System.Xml.XmlTextReader))
     {
         _reader = ((XmlTextReader)reader).Impl;
     }
     else
     {
         _reader = reader;
     }
     _preserveWhitespace = preserveWhitespace;
     if (doc == null)
     {
         throw new ArgumentException(SR.Xdom_Load_NoDocument);
     }
     if (reader == null)
     {
         throw new ArgumentException(SR.Xdom_Load_NoReader);
     }
     doc.SetBaseURI(reader.BaseURI !);
     if (reader.Settings != null &&
         reader.Settings.ValidationType == ValidationType.Schema)
     {
         doc.Schemas = reader.Settings.Schemas;
     }
     if (_reader.ReadState != ReadState.Interactive)
     {
         if (!_reader.Read())
         {
             return;
         }
     }
     LoadDocSequence(doc);
 }
コード例 #6
0
        // Set new property value.
        // Null value is passed for deleting a property.
        // While initializing, we are not assigning new values, and so the dirty flag should
        // stay untouched.
        private void RecordNewBinding(PackageXmlEnum propertyenum, object?value, bool initializing, XmlReader?reader)
        {
            // If we are reading values from the package, reader cannot be null
            Debug.Assert(!initializing || reader != null);

            if (!initializing)
            {
                _package.ThrowIfReadOnly();
            }

            // Case of an existing property.
            if (_propertyDictionary.ContainsKey(propertyenum))
            {
                // Parsing should detect redundant entries.
                if (initializing)
                {
                    throw new XmlException(SR.Format(SR.DuplicateCorePropertyName, reader !.Name),
                                           null, ((IXmlLineInfo)reader).LineNumber, ((IXmlLineInfo)reader).LinePosition);
                }

                // Nullable<DateTime> values can be checked against null
                if (value == null) // a deletion
                {
                    _propertyDictionary.Remove(propertyenum);
                }
                else // an update
                {
                    _propertyDictionary[propertyenum] = value;
                }
                // If the binding is an assignment rather than an initialization, set the dirty flag.
                _dirty = !initializing;
            }
            // Case of an initial value being set for a property. If value is null, no need to do anything
            else if (value != null)
            {
                _propertyDictionary.Add(propertyenum, value);
                // If the binding is an assignment rather than an initialization, set the dirty flag.
                _dirty = !initializing;
            }
        }
コード例 #7
0
 /// <summary>
 /// Deserializes the XML document contained by the specified XmlReader.
 /// </summary>
 /// <param name="xmlReader"></param>
 /// <typeparam name="TValue"></typeparam>
 /// <returns></returns>
 public static TValue?Deserialize <TValue>(XmlReader?xmlReader) =>
 xmlReader is null
         ? default
         : (TValue?)Deserialize(typeof(TValue), xmlReader);
コード例 #8
0
 /// <summary>
 /// Deserializes the XML document contained by the specified XmlReader.
 /// </summary>
 /// <param name="xmlReader"></param>
 /// <param name="verifyObjectName"></param>
 /// <typeparam name="TValue"></typeparam>
 /// <returns></returns>
 public static TValue?Deserialize <TValue>(XmlReader?xmlReader, bool verifyObjectName = true) =>
 xmlReader is null
         ? default
         : (TValue?)Deserialize(typeof(TValue), xmlReader, verifyObjectName);
コード例 #9
0
 public XIncludeException(string?message, XmlReader?xmlReader) : base(AddLocationInfo(message, xmlReader)) => Init(xmlReader);
コード例 #10
0
 public static object?ReadXml(this XmlReader?xmlReader, Type type) =>
 XmlHelper.Deserialize(type, xmlReader);
コード例 #11
0
 public static TValue?ReadXml <TValue>(this XmlReader?xmlReader) =>
 XmlHelper.Deserialize <TValue>(xmlReader);
コード例 #12
0
ファイル: File.cs プロジェクト: timgurke/Scriber
        /// <summary>
        /// Loads a file from the given xml reader.
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static File Load(XmlReader reader)
        {
            // init
            File?result = null;

            // locked?
            if (_Current != null)
            {
                throw new NotSupportedException();
            }

            // lock
            _Current = reader;

            // try/catch
            try
            {
                // read to first element
                while (reader.NodeType != XmlNodeType.Element && !reader.EOF)
                {
                    reader.Read();
                }

                // element present?
                if (reader.NodeType == XmlNodeType.Element)
                {
                    // namespace valid?
                    if (reader.NamespaceURI != NAMESPACE_URI)
                    {
                        throw new XmlException(string.Format("Unexpected namespace '{0}' encountered.", reader.NamespaceURI));
                    }

                    // version
                    var  version = reader.GetAttribute("version");
                    Type?type    = null;
                    switch (version)
                    {
                    case "1.0":
                        // type
                        switch (reader.LocalName)
                        {
                        case "style":
                            type = typeof(StyleFile);
                            break;

                        case "locale":
                            type = typeof(LocaleFile);
                            break;
                        }
                        break;

                    default:
                        throw new XmlException(string.Format("Version '{0}' is not supported.", version));
                    }

                    // type found?
                    if (type == null)
                    {
                        throw new XmlException(string.Format("Unexpected element '{0}' of version '{1}' encountered.", reader.LocalName, version));
                    }

                    // deserialize
                    var xs = new XmlSerializer(type);
                    xs.UnknownNode      += XmlSerializer_UnknownNode;
                    xs.UnknownAttribute += XS_UnknownAttribute;
                    result = (File)xs.Deserialize(reader);
                }
            }
            finally
            {
                // unlock
                _Current = null;
            }

            // done
            return(result ?? throw new InvalidOperationException());
        }
コード例 #13
0
 public static TValue?ReadXml <TValue>(this XmlReader?xmlReader) =>
 DataContractHelper.Deserialize <TValue>(xmlReader);