public static void Main() { //Create the validating reader. XmlTextReader txtreader = new XmlTextReader("attrs.xml"); XmlValidatingReader reader = new XmlValidatingReader(txtreader); //Read the ISBN attribute. reader.MoveToContent(); string isbn = reader.GetAttribute("ISBN"); Console.WriteLine("The ISBN value: " + isbn); //Close the reader. reader.Close(); }
public static void Main() { //Create the validating reader. XmlTextReader txtreader = new XmlTextReader("attrs.xml"); XmlValidatingReader reader = new XmlValidatingReader(txtreader); //Read the genre attribute. reader.MoveToContent(); reader.MoveToFirstAttribute(); string genre = reader.Value; Console.WriteLine("The genre value: " + genre); //Close the reader. reader.Close(); }
public static void Main() { XmlValidatingReader reader = null; XmlTextReader txtreader = null; try { //Create and load the XmlTextReader with the XML file. txtreader = new XmlTextReader("book1.xml"); txtreader.WhitespaceHandling = WhitespaceHandling.None; //Create the XmlValidatingReader over the XmlTextReader. //Set the reader to not expand general entities. reader = new XmlValidatingReader(txtreader); reader.ValidationType = ValidationType.None; reader.EntityHandling = EntityHandling.ExpandCharEntities; reader.MoveToContent(); //Move to the root element. reader.Read(); //Move to title start tag. reader.Skip(); //Skip the title element. //Read the misc start tag. The reader is now positioned on //the entity reference node. reader.ReadStartElement(); //Because EntityHandling is set to ExpandCharEntities, you must call //ResolveEntity to expand the entity. The entity replacement text is //then parsed and returned as a child node. Console.WriteLine("Expand the entity..."); reader.ResolveEntity(); Console.WriteLine("The entity replacement text is returned as a text node."); reader.Read(); Console.WriteLine("NodeType: {0} Value: {1}", reader.NodeType, reader.Value); Console.WriteLine("An EndEntity node closes the entity reference scope."); reader.Read(); Console.WriteLine("NodeType: {0} Name: {1}", reader.NodeType, reader.Name); } finally { if (reader != null) { reader.Close(); } } }
public static void Main() { XmlValidatingReader reader = null; try { //Create the XML fragment to be parsed. string xmlFrag = "<book genre='novel' misc='sale-item &h; 1987'></book>"; //Create the XmlParserContext. XmlParserContext context; string subset = "<!ENTITY h 'hardcover'>"; context = new XmlParserContext(null, null, "book", null, null, subset, "", "", XmlSpace.None); //Create the reader and set it to not expand general entities. reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context); reader.ValidationType = ValidationType.None; reader.EntityHandling = EntityHandling.ExpandCharEntities; //Read the misc attribute. Because EntityHandling is set to //ExpandCharEntities, the attribute is parsed into multiple text //and entity reference nodes. reader.MoveToContent(); reader.MoveToAttribute("misc"); while (reader.ReadAttributeValue()) { if (reader.NodeType == XmlNodeType.EntityReference) { //To expand the entity, call ResolveEntity. Console.WriteLine("{0} {1}", reader.NodeType, reader.Name); } else { Console.WriteLine("{0} {1}", reader.NodeType, reader.Value); } } } finally { if (reader != null) { reader.Close(); } } }
public static void Main(){ //Create the reader. XmlTextReader txtreader = new XmlTextReader("book4.xml"); XmlValidatingReader reader = new XmlValidatingReader(txtreader); reader.MoveToContent(); //Display each of the attribute nodes, including default attributes. while (reader.MoveToNextAttribute()){ if (reader.IsDefault) Console.Write("(default attribute) "); Console.WriteLine("{0} = {1}", reader.Name, reader.Value); } //Close the reader. reader.Close(); }
public static void Main() { XmlValidatingReader reader = null; try { //Create the string to parse. string xmlFrag = "<book genre='novel' ISBN='1-861003-78' pubdate='1987'></book> "; //Create the XmlNamespaceManager. NameTable nt = new NameTable(); XmlNamespaceManager nsmgr = new XmlNamespaceManager(nt); //Create the XmlParserContext. XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None); //Create the XmlValidatingReader . reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context); //Read the attributes on the root element. reader.MoveToContent(); if (reader.HasAttributes) { for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); Console.WriteLine("{0} = {1}", reader.Name, reader.Value); } //Move the reader back to the node that owns the attribute. reader.MoveToElement(); } } finally { if (reader != null) { reader.Close(); } } }
static void Main(string[] args) { // Der Dateiname der XML- und der Schema-Datei string xmlFileName = Path.Combine(Application.StartupPath, "Persons.xml"); string xmlSchemaFileName = Path.Combine(Application.StartupPath, "Persons.xsd"); // XmlValidatingReader mit einem neuen XmlReader als Basis erzeugen XmlValidatingReader xmlValidator = new XmlValidatingReader( new XmlTextReader(xmlFileName)); // Validier-Typ auf "Schema" festlegen xmlValidator.ValidationType = ValidationType.Schema; // Schema ohne Ziel-Namensraum hinzufügen try { xmlValidator.Schemas.Add(null, xmlSchemaFileName); } catch (Exception ex) { Console.WriteLine(ex.Message); } // Merker für die Gültigkeit des Dokuments initialisieren bool isValid = true; try { // Daten einlesen while (xmlValidator.Read()) { // Überprüfen, ob der aktuelle Knoten ein Element ist if (xmlValidator.NodeType == XmlNodeType.Element) { // Überprüfen, um welches Element es sich handelt, und // die eingelesenen Daten entsprechend ausgeben if (xmlValidator.Name == "person") { Console.WriteLine(); Console.WriteLine("Person {0}", xmlValidator.GetAttribute("id")); } else if (xmlValidator.Name == "firstname") { if (xmlValidator.MoveToContent() != XmlNodeType.None) { Console.WriteLine("Vorname: {0}", xmlValidator.ReadString()); } } else if (xmlValidator.Name == "lastname") { if (xmlValidator.MoveToContent() != XmlNodeType.None) { Console.WriteLine("Nachname: {0}", xmlValidator.ReadString()); } } else if (xmlValidator.Name == "type") { if (xmlValidator.MoveToContent() != XmlNodeType.None) { Console.WriteLine("Typ: {0}", xmlValidator.ReadString()); } } } } } catch (Exception ex) { // Fehler beim Lesen: Fehler ausgeben und Merker für die Gültigkeit des // Dokuments zurücksetzen Console.WriteLine(ex.Message); isValid = false; } // XmlValidatingReader schließen xmlValidator.Close(); // Ausgeben, ob die Datei gültig ist Console.WriteLine(); if (isValid) { Console.WriteLine("Die XML-Datei ist gültig"); } else { Console.WriteLine("Die XML-Datei ist nicht gültig"); } Console.WriteLine("Beenden mit Return"); Console.ReadLine(); }