5 // Jason Diamond (jason@injektilo.org)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) 2001, 2002 Jason Diamond http://injektilo.org/
9 // (c) 2002 Ximian, Inc. (http://www.ximian.com)
14 public abstract class XmlReader
18 protected XmlReader ()
26 public abstract int AttributeCount { get; }
28 public abstract string BaseURI { get; }
30 public virtual bool CanResolveEntity
35 public abstract int Depth { get; }
37 public abstract bool EOF { get; }
39 public virtual bool HasAttributes
41 get { return AttributeCount > 0; }
44 public abstract bool HasValue { get; }
46 public abstract bool IsDefault { get; }
48 public abstract bool IsEmptyElement { get; }
50 public abstract string this[int i] { get; }
52 public abstract string this[string name] { get; }
54 public abstract string this[
59 public abstract string LocalName { get; }
61 public abstract string Name { get; }
63 public abstract string NamespaceURI { get; }
65 public abstract XmlNameTable NameTable { get; }
67 public abstract XmlNodeType NodeType { get; }
69 public abstract string Prefix { get; }
71 public abstract char QuoteChar { get; }
73 public abstract ReadState ReadState { get; }
75 public abstract string Value { get; }
77 public abstract string XmlLang { get; }
79 public abstract XmlSpace XmlSpace { get; }
85 public abstract void Close ();
87 public abstract string GetAttribute (int i);
89 public abstract string GetAttribute (string name);
91 public abstract string GetAttribute (
93 string namespaceName);
95 public static bool IsName (string s)
99 if (s != null && s.Length > 0) {
100 char[] chars = s.ToCharArray ();
102 if (XmlConstructs.IsNameStart (chars[0])) {
104 int n = chars.Length;
106 while (i < n && XmlConstructs.IsName (chars[i]))
116 public static bool IsNameToken (string s)
120 if (s != null && s.Length > 0) {
121 char[] chars = s.ToCharArray ();
124 int n = chars.Length;
126 while (i < n && XmlConstructs.IsName (chars[i]))
135 public virtual bool IsStartElement ()
137 return (MoveToContent () == XmlNodeType.Element);
140 public virtual bool IsStartElement (string name)
142 if (!IsStartElement ())
145 return (Name == name);
148 public virtual bool IsStartElement (string localName, string namespaceName)
150 if (!IsStartElement ())
153 return (LocalName == localName && NamespaceURI == namespaceName);
156 public abstract string LookupNamespace (string prefix);
158 public abstract void MoveToAttribute (int i);
160 public abstract bool MoveToAttribute (string name);
162 public abstract bool MoveToAttribute (
164 string namespaceName);
166 private bool IsContent (XmlNodeType nodeType)
169 * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
172 case XmlNodeType.Text:
174 case XmlNodeType.CDATA:
176 case XmlNodeType.Element:
178 case XmlNodeType.EndElement:
180 case XmlNodeType.EntityReference:
182 case XmlNodeType.EndEntity:
189 public virtual XmlNodeType MoveToContent ()
194 case XmlNodeType.XmlDeclaration:
195 case XmlNodeType.DocumentType:
196 case XmlNodeType.ProcessingInstruction:
197 case XmlNodeType.Comment:
198 case XmlNodeType.Whitespace:
199 case XmlNodeType.SignificantWhitespace:
206 } while (!EOF && loop);
207 return XmlNodeType.None;
210 public abstract bool MoveToElement ();
212 public abstract bool MoveToFirstAttribute ();
214 public abstract bool MoveToNextAttribute ();
216 public abstract bool Read ();
218 public abstract bool ReadAttributeValue ();
220 public virtual string ReadElementString ()
222 if (MoveToContent () != XmlNodeType.Element) {
223 string error = String.Format ("'{0}' is an invalid node type.",
224 NodeType.ToString ());
225 throw new XmlException (this as IXmlLineInfo, error);
228 string result = String.Empty;
229 if (!IsEmptyElement) {
231 result = ReadString ();
232 if (NodeType != XmlNodeType.EndElement) {
233 string error = String.Format ("'{0}' is an invalid node type.",
234 NodeType.ToString ());
235 throw new XmlException (this as IXmlLineInfo, error);
243 public virtual string ReadElementString (string name)
245 if (MoveToContent () != XmlNodeType.Element) {
246 string error = String.Format ("'{0}' is an invalid node type.",
247 NodeType.ToString ());
248 throw new XmlException (this as IXmlLineInfo, error);
252 string error = String.Format ("The {0} tag from namespace {1} is expected.",
254 throw new XmlException (this as IXmlLineInfo, error);
257 string result = String.Empty;
258 if (!IsEmptyElement) {
260 result = ReadString ();
261 if (NodeType != XmlNodeType.EndElement) {
262 string error = String.Format ("'{0}' is an invalid node type.",
263 NodeType.ToString ());
264 throw new XmlException (this as IXmlLineInfo, error);
272 public virtual string ReadElementString (string localName, string namespaceName)
274 if (MoveToContent () != XmlNodeType.Element) {
275 string error = String.Format ("'{0}' is an invalid node type.",
276 NodeType.ToString ());
277 throw new XmlException (this as IXmlLineInfo, error);
280 if (localName != LocalName || NamespaceURI != namespaceName) {
281 string error = String.Format ("The {0} tag from namespace {1} is expected.",
282 LocalName, NamespaceURI);
283 throw new XmlException (this as IXmlLineInfo, error);
286 string result = String.Empty;
287 if (!IsEmptyElement) {
289 result = ReadString ();
290 if (NodeType != XmlNodeType.EndElement) {
291 string error = String.Format ("'{0}' is an invalid node type.",
292 NodeType.ToString ());
293 throw new XmlException (this as IXmlLineInfo, error);
301 public virtual void ReadEndElement ()
303 if (MoveToContent () != XmlNodeType.EndElement) {
304 string error = String.Format ("'{0}' is an invalid node type.",
305 NodeType.ToString ());
306 throw new XmlException (this as IXmlLineInfo, error);
312 public abstract string ReadInnerXml ();
314 public abstract string ReadOuterXml ();
316 public virtual void ReadStartElement ()
318 if (MoveToContent () != XmlNodeType.Element) {
319 string error = String.Format ("'{0}' is an invalid node type.",
320 NodeType.ToString ());
321 throw new XmlException (this as IXmlLineInfo, error);
327 public virtual void ReadStartElement (string name)
329 if (MoveToContent () != XmlNodeType.Element) {
330 string error = String.Format ("'{0}' is an invalid node type.",
331 NodeType.ToString ());
332 throw new XmlException (this as IXmlLineInfo, error);
336 string error = String.Format ("The {0} tag from namespace {1} is expected.",
338 throw new XmlException (this as IXmlLineInfo, error);
344 public virtual void ReadStartElement (string localName, string namespaceName)
346 if (MoveToContent () != XmlNodeType.Element) {
347 string error = String.Format ("'{0}' is an invalid node type.",
348 NodeType.ToString ());
349 throw new XmlException (this as IXmlLineInfo, error);
352 if (localName != LocalName || NamespaceURI != namespaceName) {
353 string error = String.Format ("The {0} tag from namespace {1} is expected.",
354 LocalName, NamespaceURI);
355 throw new XmlException (this as IXmlLineInfo, error);
361 public abstract string ReadString ();
363 public abstract void ResolveEntity ();
365 public virtual void Skip ()
367 if (ReadState != ReadState.Interactive)
371 if (NodeType != XmlNodeType.Element || IsEmptyElement) {
377 while (Read() && depth < Depth);
378 if (NodeType == XmlNodeType.EndElement)