using System; using System.IO; using System.Xml; using System.Collections; namespace iTextSharp.text.xml { /// /// The ParserBase-class provides XML document parsing. /// public abstract class ParserBase { public void Parse(XmlDocument xDoc) { string xml = xDoc.OuterXml; StringReader stringReader = new StringReader(xml); XmlTextReader reader = new XmlTextReader(stringReader); this.Parse(reader); } public void Parse(XmlTextReader reader) { try { while (reader.Read()) { switch (reader.NodeType) { case XmlNodeType.Element: string namespaceURI = reader.NamespaceURI; string name = reader.Name; bool isEmpty = reader.IsEmptyElement; Hashtable attributes = new Hashtable(); if (reader.HasAttributes) { for (int i = 0; i < reader.AttributeCount; i++) { reader.MoveToAttribute(i); attributes.Add(reader.Name,reader.Value); } } this.StartElement(namespaceURI, name, name, attributes); if (isEmpty) { EndElement(namespaceURI, name, name); } break; case XmlNodeType.EndElement: EndElement(reader.NamespaceURI, reader.Name, reader.Name); break; case XmlNodeType.Text: Characters(reader.Value, 0, reader.Value.Length); break; // There are many other types of nodes, but // we are not interested in them case XmlNodeType.Whitespace: Characters(reader.Value, 0, reader.Value.Length); break; } } } catch (XmlException e) { Console.WriteLine(e.Message); } finally { if (reader != null) { reader.Close(); } } } /// /// Begins the process of processing an XML document /// /// the XML document to parse public void Parse(string url) { XmlTextReader reader = null; reader = new XmlTextReader(url); this.Parse(reader); } /// /// This method gets called when a start tag is encountered. /// /// /// /// the name of the tag that is encountered /// the list of attributes public abstract void StartElement(String uri, String lname, String name, Hashtable attrs); /// /// This method gets called when an end tag is encountered. /// /// /// /// the name of the tag that ends public abstract void EndElement(String uri, String lname, String name); /// /// This method gets called when characters are encountered. /// /// an array of characters /// the start position in the array /// the number of characters to read from the array public abstract void Characters(string content, int start, int length); } }