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 (XmlChar.IsFirstNameChar (chars[0])) {
104 int n = chars.Length;
106 while (i < n && XmlChar.IsNameChar (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 && XmlChar.IsNameChar (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 ()
192 XmlNodeType nodeType = NodeType;
193 if (IsContent (nodeType))
197 return XmlNodeType.None;
200 public abstract bool MoveToElement ();
202 public abstract bool MoveToFirstAttribute ();
204 public abstract bool MoveToNextAttribute ();
206 public abstract bool Read ();
208 public abstract bool ReadAttributeValue ();
210 public virtual string ReadElementString ()
212 if (MoveToContent () != XmlNodeType.Element) {
213 string error = String.Format ("'{0}' is an invalid node type.",
214 NodeType.ToString ());
215 throw new XmlException (this as IXmlLineInfo, error);
218 string result = String.Empty;
219 if (!IsEmptyElement) {
221 result = ReadString ();
222 if (NodeType != XmlNodeType.EndElement) {
223 string error = String.Format ("'{0}' is an invalid node type.",
224 NodeType.ToString ());
225 throw new XmlException (this as IXmlLineInfo, error);
233 public virtual string ReadElementString (string name)
235 if (MoveToContent () != XmlNodeType.Element) {
236 string error = String.Format ("'{0}' is an invalid node type.",
237 NodeType.ToString ());
238 throw new XmlException (this as IXmlLineInfo, error);
242 string error = String.Format ("The {0} tag from namespace {1} is expected.",
244 throw new XmlException (this as IXmlLineInfo, error);
247 string result = String.Empty;
248 if (!IsEmptyElement) {
250 result = ReadString ();
251 if (NodeType != XmlNodeType.EndElement) {
252 string error = String.Format ("'{0}' is an invalid node type.",
253 NodeType.ToString ());
254 throw new XmlException (this as IXmlLineInfo, error);
262 public virtual string ReadElementString (string localName, string namespaceName)
264 if (MoveToContent () != XmlNodeType.Element) {
265 string error = String.Format ("'{0}' is an invalid node type.",
266 NodeType.ToString ());
267 throw new XmlException (this as IXmlLineInfo, error);
270 if (localName != LocalName || NamespaceURI != namespaceName) {
271 string error = String.Format ("The {0} tag from namespace {1} is expected.",
272 LocalName, NamespaceURI);
273 throw new XmlException (this as IXmlLineInfo, error);
276 string result = String.Empty;
277 if (!IsEmptyElement) {
279 result = ReadString ();
280 if (NodeType != XmlNodeType.EndElement) {
281 string error = String.Format ("'{0}' is an invalid node type.",
282 NodeType.ToString ());
283 throw new XmlException (this as IXmlLineInfo, error);
291 public virtual void ReadEndElement ()
293 if (MoveToContent () != XmlNodeType.EndElement) {
294 string error = String.Format ("'{0}' is an invalid node type.",
295 NodeType.ToString ());
296 throw new XmlException (this as IXmlLineInfo, error);
302 public abstract string ReadInnerXml ();
304 public abstract string ReadOuterXml ();
306 public virtual void ReadStartElement ()
308 if (MoveToContent () != XmlNodeType.Element) {
309 string error = String.Format ("'{0}' is an invalid node type.",
310 NodeType.ToString ());
311 throw new XmlException (this as IXmlLineInfo, error);
317 public virtual void ReadStartElement (string name)
319 if (MoveToContent () != XmlNodeType.Element) {
320 string error = String.Format ("'{0}' is an invalid node type.",
321 NodeType.ToString ());
322 throw new XmlException (this as IXmlLineInfo, error);
326 string error = String.Format ("The {0} tag from namespace {1} is expected.",
328 throw new XmlException (this as IXmlLineInfo, error);
334 public virtual void ReadStartElement (string localName, string namespaceName)
336 if (MoveToContent () != XmlNodeType.Element) {
337 string error = String.Format ("'{0}' is an invalid node type.",
338 NodeType.ToString ());
339 throw new XmlException (this as IXmlLineInfo, error);
342 if (localName != LocalName || NamespaceURI != namespaceName) {
343 string error = String.Format ("The {0} tag from namespace {1} is expected.",
344 LocalName, NamespaceURI);
345 throw new XmlException (this as IXmlLineInfo, error);
351 public abstract string ReadString ();
353 public abstract void ResolveEntity ();
355 public virtual void Skip ()
357 if (ReadState != ReadState.Interactive)
361 if (NodeType != XmlNodeType.Element || IsEmptyElement) {
367 while (Read() && depth < Depth);
368 if (NodeType == XmlNodeType.EndElement)