public void StartParsing(XmlReader reader, string targetNamespace) { this.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); string code; if (!CheckSchemaRoot(rootType, out 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; } }
internal XDeclaration(XmlReader r) { _version = r.GetAttribute("version"); _encoding = r.GetAttribute("encoding"); _standalone = r.GetAttribute("standalone"); r.Read(); }
public bool CheckIgnores(XmlReader reader, bool pi, bool comm, bool ws) { if (reader == null) return false; _commentCount = 0; _piCount = 0; _wsCount = 0; while (reader.Read()) { if (XmlNodeType.ProcessingInstruction == reader.NodeType) { ++_piCount; } if (XmlNodeType.SignificantWhitespace == reader.NodeType) { ++_wsCount; } if (XmlNodeType.Comment == reader.NodeType) { ++_commentCount; } } if (pi && _piCount > 0) { CError.WriteLineIgnore("ProcessingInstruction Found"); CError.WriteLineIgnore(reader.Name); CError.WriteLineIgnore(">" + reader.Value + "<"); return false; } if (comm && _commentCount > 0) { CError.WriteLineIgnore("Comment Found"); CError.WriteLineIgnore(reader.Name); CError.WriteLineIgnore(">" + reader.Value + "<"); return false; } if (ws && _wsCount > 0) { CError.WriteLineIgnore("Significant Whitespace Found"); CError.WriteLineIgnore(reader.Name); CError.WriteLineIgnore(">" + reader.Value + "<"); return false; } return true; }
protected void ConsumeReader(XmlReader reader) { while (reader.Read()) { string x = reader.Name + reader.NodeType + reader.Value; if (reader.NodeType == XmlNodeType.Element) { if (reader.HasAttributes) { reader.MoveToFirstAttribute(); int index = 0; reader.MoveToAttribute(index); index++; while (reader.MoveToNextAttribute()) { string name = reader.Name; string value; value = reader.GetAttribute(index); value = reader.GetAttribute(name); value = reader.GetAttribute(name, null); reader.ReadAttributeValue(); reader.MoveToAttribute(index); reader.MoveToAttribute(name, null); index++; } } } if (reader.NodeType == XmlNodeType.EndElement) { reader.Skip(); } } }
public SchemaType Parse(XmlReader reader, string targetNamespace) { StartParsing(reader, targetNamespace); while (ParseReaderNode() && reader.Read()) { } return FinishParsing(); }
private XmlReader NestRead(XmlReader r) { r.Read(); r.Read(); if (!(r.Name == "elem0" && r.NodeType == XmlNodeType.Element)) { CError.WriteLine(r.Name); NestRead(r.ReadSubtree()); } r.Dispose(); return r; }
/// <summary> /// Create a writer that can be used to create nodes in this document. The root node will be assigned "baseUri", and flags /// can be passed to indicate that names should be atomized by the builder and/or a fragment should be created. /// </summary> internal void LoadFromReader(XmlReader reader, XmlSpace space) { XPathDocumentBuilder builder; IXmlLineInfo lineInfo; string xmlnsUri; bool topLevelReader; int initialDepth; if (reader == null) throw new ArgumentNullException(nameof(reader)); // Determine line number provider lineInfo = reader as IXmlLineInfo; if (lineInfo == null || !lineInfo.HasLineInfo()) lineInfo = null; _hasLineInfo = (lineInfo != null); _nameTable = reader.NameTable; builder = new XPathDocumentBuilder(this, lineInfo, reader.BaseURI, LoadFlags.None); try { // Determine whether reader is in initial state topLevelReader = (reader.ReadState == ReadState.Initial); initialDepth = reader.Depth; // Get atomized xmlns uri Debug.Assert((object)_nameTable.Get(string.Empty) == (object)string.Empty, "NameTable must contain atomized string.Empty"); xmlnsUri = _nameTable.Get(XmlReservedNs.NsXmlNs); // Read past Initial state; if there are no more events then load is complete if (topLevelReader && !reader.Read()) return; // Read all events do { // If reader began in intermediate state, return when all siblings have been read if (!topLevelReader && reader.Depth < initialDepth) return; switch (reader.NodeType) { case XmlNodeType.Element: { bool isEmptyElement = reader.IsEmptyElement; builder.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI, reader.BaseURI); // Add attribute and namespace nodes to element while (reader.MoveToNextAttribute()) { string namespaceUri = reader.NamespaceURI; if ((object)namespaceUri == (object)xmlnsUri) { if (reader.Prefix.Length == 0) { // Default namespace declaration "xmlns" Debug.Assert(reader.LocalName == "xmlns"); builder.WriteNamespaceDeclaration(string.Empty, reader.Value); } else { Debug.Assert(reader.Prefix == "xmlns"); builder.WriteNamespaceDeclaration(reader.LocalName, reader.Value); } } else { builder.WriteStartAttribute(reader.Prefix, reader.LocalName, namespaceUri); builder.WriteString(reader.Value, TextBlockType.Text); builder.WriteEndAttribute(); } } if (isEmptyElement) builder.WriteEndElement(true); break; } case XmlNodeType.EndElement: builder.WriteEndElement(false); break; case XmlNodeType.Text: case XmlNodeType.CDATA: builder.WriteString(reader.Value, TextBlockType.Text); break; case XmlNodeType.SignificantWhitespace: if (reader.XmlSpace == XmlSpace.Preserve) builder.WriteString(reader.Value, TextBlockType.SignificantWhitespace); else // Significant whitespace without xml:space="preserve" is not significant in XPath/XQuery data model goto case XmlNodeType.Whitespace; break; case XmlNodeType.Whitespace: // We intentionally ignore the reader.XmlSpace property here and blindly trust // the reported node type. If the reported information is not in sync // (in this case if the reader.XmlSpace == Preserve) then we make the choice // to trust the reported node type. Since we have no control over the input reader // we can't even assert here. // Always filter top-level whitespace if (space == XmlSpace.Preserve && (!topLevelReader || reader.Depth != 0)) builder.WriteString(reader.Value, TextBlockType.Whitespace); break; case XmlNodeType.Comment: builder.WriteComment(reader.Value); break; case XmlNodeType.ProcessingInstruction: builder.WriteProcessingInstruction(reader.LocalName, reader.Value, reader.BaseURI); break; case XmlNodeType.EntityReference: reader.ResolveEntity(); break; case XmlNodeType.DocumentType: // Create ID tables IDtdInfo info = reader.DtdInfo; if (info != null) builder.CreateIdTables(info); break; case XmlNodeType.EndEntity: case XmlNodeType.None: case XmlNodeType.XmlDeclaration: break; } } while (reader.Read()); } finally { builder.Close(); } }
internal static void BuildSubtree(XmlReader reader, XmlWriter writer) { // important (perf) string literal... string xmlnsUri = XmlConst.ReservedNsXmlNs; // http://www.w3.org/2000/xmlns/ ReadState readState = reader.ReadState; if (readState != ReadState.Initial && readState != ReadState.Interactive) { throw new ArgumentException(SR.Xml_InvalidOperation, "reader"); } int level = 0; if (readState == ReadState.Initial) { if (!reader.Read()) return; level++; // if start in initial, read everything (not just first) } do { switch (reader.NodeType) { case XmlNodeType.Element: writer.WriteStartElement(reader.Prefix, reader.LocalName, reader.NamespaceURI); bool isEmptyElement = reader.IsEmptyElement; while (reader.MoveToNextAttribute()) { if ((object)reader.NamespaceURI == (object)xmlnsUri) { if (reader.Prefix.Length == 0) { // Default namespace declaration "xmlns" Debug.Assert(reader.LocalName == "xmlns"); writer.WriteAttributeString("", "xmlns", xmlnsUri, reader.Value); } else { Debug.Assert(reader.Prefix == "xmlns"); writer.WriteAttributeString("xmlns", reader.LocalName, xmlnsUri, reader.Value); } } else { writer.WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); writer.WriteString(reader.Value); writer.WriteEndAttribute(); } } reader.MoveToElement(); if (isEmptyElement) { // there might still be a value, if there is a default value specified in the schema writer.WriteEndElement(); } else { level++; } break; case XmlNodeType.EndElement: writer.WriteFullEndElement(); //should not read beyond the level of the reader's original position. level--; break; case XmlNodeType.Text: case XmlNodeType.CDATA: writer.WriteString(reader.Value); break; case XmlNodeType.SignificantWhitespace: case XmlNodeType.Whitespace: writer.WriteString(reader.Value); break; case XmlNodeType.Comment: writer.WriteComment(reader.Value); break; case XmlNodeType.ProcessingInstruction: writer.WriteProcessingInstruction(reader.LocalName, reader.Value); break; case XmlNodeType.EntityReference: reader.ResolveEntity(); break; case XmlNodeType.EndEntity: case XmlNodeType.None: case XmlNodeType.DocumentType: case XmlNodeType.XmlDeclaration: break; case XmlNodeType.Attribute: if ((object)reader.NamespaceURI == (object)xmlnsUri) { if (reader.Prefix.Length == 0) { // Default namespace declaration "xmlns" Debug.Assert(reader.LocalName == "xmlns"); writer.WriteAttributeString("", "xmlns", xmlnsUri, reader.Value); } else { Debug.Assert(reader.Prefix == "xmlns"); writer.WriteAttributeString("xmlns", reader.LocalName, xmlnsUri, reader.Value); } } else { writer.WriteStartAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); writer.WriteString(reader.Value); writer.WriteEndAttribute(); } break; } } while (reader.Read() && (level > 0)); }
private void ReadElementFrom(XmlReader r, LoadOptions o) { if (r.ReadState != ReadState.Interactive) throw new InvalidOperationException(SR.InvalidOperation_ExpectedInteractive); name = XNamespace.Get(r.NamespaceURI).GetName(r.LocalName); if ((o & LoadOptions.SetBaseUri) != 0) { string baseUri = r.BaseURI; if (!string.IsNullOrEmpty(baseUri)) { SetBaseUri(baseUri); } } IXmlLineInfo li = null; if ((o & LoadOptions.SetLineInfo) != 0) { li = r as IXmlLineInfo; if (li != null && li.HasLineInfo()) { SetLineInfo(li.LineNumber, li.LinePosition); } } if (r.MoveToFirstAttribute()) { do { XAttribute a = new XAttribute(XNamespace.Get(r.Prefix.Length == 0 ? string.Empty : r.NamespaceURI).GetName(r.LocalName), r.Value); if (li != null && li.HasLineInfo()) { a.SetLineInfo(li.LineNumber, li.LinePosition); } AppendAttributeSkipNotify(a); } while (r.MoveToNextAttribute()); r.MoveToElement(); } if (!r.IsEmptyElement) { r.Read(); ReadContentFrom(r, o); } r.Read(); }
private void ReadChildNodes( XPathContainer parent, string parentBaseUri, XmlReader reader, PositionInfo positionInfo ) { do { documentIndex++; switch( reader.NodeType ) { case XmlNodeType.Element: { string baseUri = reader.BaseURI; XPathElement e = null; if( reader.IsEmptyElement ) { e = new XPathEmptyElement( reader.Prefix, reader.LocalName, reader.NamespaceURI, positionInfo.LineNumber, positionInfo.LinePosition, parent.topNamespace, documentIndex ); ReadAttributes( e, reader ); } else { e = new XPathElement( reader.Prefix, reader.LocalName, reader.NamespaceURI, positionInfo.LineNumber, positionInfo.LinePosition, parent.topNamespace, documentIndex ); ReadAttributes( e, reader ); reader.Read(); ReadChildNodes( e, baseUri, reader, positionInfo ); } if (parentBaseUri != baseUri) { // We can't user Ref.Equial Because Reader fails to fully atomize base Uri. if (elementBaseUriMap == null) { elementBaseUriMap = new Hashtable(); } elementBaseUriMap[e] = baseUri; } parent.AppendChild( e ); break; } case XmlNodeType.Comment: parent.AppendChild( new XPathComment( reader.Value, documentIndex ) ); break; case XmlNodeType.ProcessingInstruction: parent.AppendChild( new XPathProcessingInstruction( reader.LocalName, reader.Value, documentIndex ) ); break; case XmlNodeType.SignificantWhitespace: if( reader.XmlSpace == XmlSpace.Preserve ) { parent.AppendSignificantWhitespace( reader.Value, positionInfo.LineNumber, positionInfo.LinePosition, documentIndex ); } else { // SWS without xml:space='preserve' is not really significant for XPath. // so we treat it as just WS goto case XmlNodeType.Whitespace; } break; case XmlNodeType.Whitespace: if( space == XmlSpace.Preserve ) { parent.AppendWhitespace( reader.Value, positionInfo.LineNumber, positionInfo.LinePosition, documentIndex ); } break; case XmlNodeType.CDATA: case XmlNodeType.Text: parent.AppendText( reader.Value, positionInfo.LineNumber, positionInfo.LinePosition, documentIndex ); break; case XmlNodeType.EntityReference: reader.ResolveEntity(); reader.Read(); ReadChildNodes( parent, parentBaseUri, reader, positionInfo); break; case XmlNodeType.EndEntity: case XmlNodeType.EndElement: case XmlNodeType.None: return; case XmlNodeType.DocumentType: XmlValidatingReader vr = reader as XmlValidatingReader; if ( vr != null ) { SchemaInfo info = vr.GetSchemaInfo(); if ( info != null ) { GetIDInfo( info); } } break; case XmlNodeType.XmlDeclaration: default: break; } }while( reader.Read() ); }
private void Load( XmlReader reader ) { nt = reader.NameTable; baseURI = reader.BaseURI; Debug.Assert(baseURI != null, "uri can't be null, isn't it?"); xmlnsUri = (nt != null ? nt.Add(XmlReservedNs.NsXmlNs) : XmlReservedNs.NsXmlNs); PositionInfo positionInfo = PositionInfo.GetPositionInfo(reader); if ( reader.ReadState == ReadState.Initial ) { if ( !reader.Read() ) return; } ReadChildNodes( root, baseURI, reader, positionInfo ); }
private static object DeserializeArray(XmlReader xmlReader) { string startingElementName = xmlReader.Name; xmlReader.Read(); string arrayType = xmlReader.Name; ArrayList array = new ArrayList(); Type returnType = null; while (xmlReader.Name != startingElementName) { string tempValue = xmlReader.ReadElementString(); switch (arrayType) { case "boolean": returnType = returnType ?? typeof(bool); array.Add(tempValue == "true"); break; // TODO: this is not an array but a base64 encoded string //case "byte": // //returnType = returnType ?? typeof(bool); // //return Convert.FromBase64String(tempValue); // //throw new Exception("Should never reach this"); // break; case "byte": returnType = returnType ?? typeof(sbyte); array.Add(Convert.ToSByte(tempValue)); break; case "char": returnType = returnType ?? typeof(char); array.Add(Convert.ToChar(Convert.ToByte(tempValue))); break; case "dateTime": returnType = returnType ?? typeof(DateTime); array.Add(GetDateTimeFromString(tempValue)); break; case "double": returnType = returnType ?? typeof(double); array.Add(Convert.ToDouble(tempValue)); break; case "guid": returnType = returnType ?? typeof(Guid); array.Add(GetGuidFromString(tempValue)); break; case "short": returnType = returnType ?? typeof(short); array.Add(Convert.ToInt16(tempValue)); break; case "unsignedShort": returnType = returnType ?? typeof(ushort); array.Add(Convert.ToUInt16(tempValue)); break; case "int": returnType = returnType ?? typeof(int); array.Add(Convert.ToInt32(tempValue)); break; case "unsignedInt": returnType = returnType ?? typeof(uint); array.Add(Convert.ToUInt32(tempValue)); break; case "long": returnType = returnType ?? typeof(long); array.Add(Convert.ToInt64(tempValue)); break; case "unsignedLong": returnType = returnType ?? typeof(ulong); array.Add(Convert.ToUInt64(tempValue)); break; case "float": returnType = returnType ?? typeof(float); array.Add((Single)Convert.ToDouble(tempValue)); break; case "string": returnType = returnType ?? typeof(string); array.Add(tempValue); break; default: returnType = returnType ?? typeof(object); break; } } return array.ToArray(returnType); }