static void ReadNode(XmlReader reader, XmlDocument document, bool ignoreComments) { switch (reader.NodeType) { case XmlNodeType.DocumentType: if (document.DocumentElement?.HasChildNodes == true) { return; } string docTypeSystem = reader.GetAttribute("system"); string docPublicId = reader.GetAttribute("public"); XmlDocumentType docType = document.CreateDocumentType(reader.Name, docPublicId, docTypeSystem, reader.Value); document.AppendChild(docType); break; case XmlNodeType.XmlDeclaration: if (document.DocumentElement?.HasChildNodes == true) { return; } string docVersion = reader.GetAttribute("version") ?? "1.0"; string docEncoding = reader.GetAttribute("encoding"); string docStandAlone = reader.GetAttribute("standalone"); XmlDeclaration declaration = document.CreateXmlDeclaration(docVersion, docEncoding, docStandAlone); document.AppendChild(declaration); break; case XmlNodeType.Element: XmlElement child = document.CreateElement(reader.Prefix, reader.LocalName, reader.NamespaceURI); reader.CollectAttributes(child); document.AppendChild(child); reader.CollectChildren(child, ignoreComments); break; case XmlNodeType.Attribute: XmlAttribute attribute = document.CreateAttribute(reader.Prefix, reader.LocalName, reader.NamespaceURI); if (reader.HasValue) { attribute.Value = reader.Value; } document.AppendChild(attribute); break; case XmlNodeType.Text: document.AppendChild(document.CreateTextNode(reader.Value)); break; case XmlNodeType.CDATA: document.AppendChild(document.CreateCDataSection(reader.Value)); break; case XmlNodeType.ProcessingInstruction: document.AppendChild(document.CreateProcessingInstruction(reader.Name, reader.Value)); break; case XmlNodeType.Comment: if (!ignoreComments) { document.AppendChild(document.CreateComment(reader.Value)); } break; case XmlNodeType.EntityReference: document.AppendChild(document.CreateEntityReference(reader.Name)); break; } }