XmlReader.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. //
  2. // XmlReader.cs
  3. //
  4. // Authors:
  5. // Jason Diamond ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2001, 2002 Jason Diamond http://injektilo.org/
  9. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  10. //
  11. namespace System.Xml
  12. {
  13. public abstract class XmlReader
  14. {
  15. #region Constructor
  16. protected XmlReader ()
  17. {
  18. }
  19. #endregion
  20. #region Properties
  21. public abstract int AttributeCount { get; }
  22. public abstract string BaseURI { get; }
  23. public virtual bool CanResolveEntity
  24. {
  25. get { return false; }
  26. }
  27. public abstract int Depth { get; }
  28. public abstract bool EOF { get; }
  29. public virtual bool HasAttributes
  30. {
  31. get { return AttributeCount > 0; }
  32. }
  33. public abstract bool HasValue { get; }
  34. public abstract bool IsDefault { get; }
  35. public abstract bool IsEmptyElement { get; }
  36. public abstract string this[int i] { get; }
  37. public abstract string this[string name] { get; }
  38. public abstract string this[
  39. string localName,
  40. string namespaceName]
  41. { get; }
  42. public abstract string LocalName { get; }
  43. public abstract string Name { get; }
  44. public abstract string NamespaceURI { get; }
  45. public abstract XmlNameTable NameTable { get; }
  46. public abstract XmlNodeType NodeType { get; }
  47. public abstract string Prefix { get; }
  48. public abstract char QuoteChar { get; }
  49. public abstract ReadState ReadState { get; }
  50. public abstract string Value { get; }
  51. public abstract string XmlLang { get; }
  52. public abstract XmlSpace XmlSpace { get; }
  53. #endregion
  54. #region Methods
  55. public abstract void Close ();
  56. public abstract string GetAttribute (int i);
  57. public abstract string GetAttribute (string name);
  58. public abstract string GetAttribute (
  59. string localName,
  60. string namespaceName);
  61. public static bool IsName (string s)
  62. {
  63. bool result = false;
  64. if (s != null && s.Length > 0) {
  65. char[] chars = s.ToCharArray ();
  66. if (XmlConstructs.IsNameStart (chars[0])) {
  67. int i = 1;
  68. int n = chars.Length;
  69. while (i < n && XmlConstructs.IsName (chars[i]))
  70. ++i;
  71. result = i == n;
  72. }
  73. }
  74. return result;
  75. }
  76. public static bool IsNameToken (string s)
  77. {
  78. bool result = false;
  79. if (s != null && s.Length > 0) {
  80. char[] chars = s.ToCharArray ();
  81. int i = 0;
  82. int n = chars.Length;
  83. while (i < n && XmlConstructs.IsName (chars[i]))
  84. ++i;
  85. result = i == n;
  86. }
  87. return result;
  88. }
  89. public virtual bool IsStartElement ()
  90. {
  91. return (MoveToContent () == XmlNodeType.Element);
  92. }
  93. public virtual bool IsStartElement (string name)
  94. {
  95. if (!IsStartElement ())
  96. return false;
  97. return (Name == name);
  98. }
  99. public virtual bool IsStartElement (string localName, string namespaceName)
  100. {
  101. if (!IsStartElement ())
  102. return false;
  103. return (LocalName == localName && NamespaceURI == namespaceName);
  104. }
  105. public abstract string LookupNamespace (string prefix);
  106. public abstract void MoveToAttribute (int i);
  107. public abstract bool MoveToAttribute (string name);
  108. public abstract bool MoveToAttribute (
  109. string localName,
  110. string namespaceName);
  111. private bool IsContent (XmlNodeType nodeType)
  112. {
  113. /* MS doc says:
  114. * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
  115. */
  116. switch (nodeType) {
  117. case XmlNodeType.Text:
  118. return true;
  119. case XmlNodeType.CDATA:
  120. return true;
  121. case XmlNodeType.Element:
  122. return true;
  123. case XmlNodeType.EndElement:
  124. return true;
  125. case XmlNodeType.EntityReference:
  126. return true;
  127. case XmlNodeType.EndEntity:
  128. return true;
  129. }
  130. return false;
  131. }
  132. public virtual XmlNodeType MoveToContent ()
  133. {
  134. do {
  135. XmlNodeType nodeType = NodeType;
  136. if (IsContent (nodeType))
  137. return nodeType;
  138. } while (Read ());
  139. return XmlNodeType.None;
  140. }
  141. public abstract bool MoveToElement ();
  142. public abstract bool MoveToFirstAttribute ();
  143. public abstract bool MoveToNextAttribute ();
  144. public abstract bool Read ();
  145. public abstract bool ReadAttributeValue ();
  146. public virtual string ReadElementString ()
  147. {
  148. if (MoveToContent () != XmlNodeType.Element) {
  149. string error = String.Format ("'{0}' is an invalid node type.",
  150. NodeType.ToString ());
  151. throw new XmlException (this as IXmlLineInfo, error);
  152. }
  153. string result = String.Empty;
  154. if (!IsEmptyElement) {
  155. Read ();
  156. result = ReadString ();
  157. if (NodeType != XmlNodeType.EndElement) {
  158. string error = String.Format ("'{0}' is an invalid node type.",
  159. NodeType.ToString ());
  160. throw new XmlException (this as IXmlLineInfo, error);
  161. }
  162. }
  163. Read ();
  164. return result;
  165. }
  166. public virtual string ReadElementString (string name)
  167. {
  168. if (MoveToContent () != XmlNodeType.Element) {
  169. string error = String.Format ("'{0}' is an invalid node type.",
  170. NodeType.ToString ());
  171. throw new XmlException (this as IXmlLineInfo, error);
  172. }
  173. if (name != Name) {
  174. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  175. Name, NamespaceURI);
  176. throw new XmlException (this as IXmlLineInfo, error);
  177. }
  178. string result = String.Empty;
  179. if (!IsEmptyElement) {
  180. Read ();
  181. result = ReadString ();
  182. if (NodeType != XmlNodeType.EndElement) {
  183. string error = String.Format ("'{0}' is an invalid node type.",
  184. NodeType.ToString ());
  185. throw new XmlException (this as IXmlLineInfo, error);
  186. }
  187. }
  188. Read ();
  189. return result;
  190. }
  191. public virtual string ReadElementString (string localName, string namespaceName)
  192. {
  193. if (MoveToContent () != XmlNodeType.Element) {
  194. string error = String.Format ("'{0}' is an invalid node type.",
  195. NodeType.ToString ());
  196. throw new XmlException (this as IXmlLineInfo, error);
  197. }
  198. if (localName != LocalName || NamespaceURI != namespaceName) {
  199. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  200. LocalName, NamespaceURI);
  201. throw new XmlException (this as IXmlLineInfo, error);
  202. }
  203. string result = String.Empty;
  204. if (!IsEmptyElement) {
  205. Read ();
  206. result = ReadString ();
  207. if (NodeType != XmlNodeType.EndElement) {
  208. string error = String.Format ("'{0}' is an invalid node type.",
  209. NodeType.ToString ());
  210. throw new XmlException (this as IXmlLineInfo, error);
  211. }
  212. }
  213. Read ();
  214. return result;
  215. }
  216. public virtual void ReadEndElement ()
  217. {
  218. if (MoveToContent () != XmlNodeType.EndElement) {
  219. string error = String.Format ("'{0}' is an invalid node type.",
  220. NodeType.ToString ());
  221. throw new XmlException (this as IXmlLineInfo, error);
  222. }
  223. Read ();
  224. }
  225. public abstract string ReadInnerXml ();
  226. public abstract string ReadOuterXml ();
  227. public virtual void ReadStartElement ()
  228. {
  229. if (MoveToContent () != XmlNodeType.Element) {
  230. string error = String.Format ("'{0}' is an invalid node type.",
  231. NodeType.ToString ());
  232. throw new XmlException (this as IXmlLineInfo, error);
  233. }
  234. Read ();
  235. }
  236. public virtual void ReadStartElement (string name)
  237. {
  238. if (MoveToContent () != XmlNodeType.Element) {
  239. string error = String.Format ("'{0}' is an invalid node type.",
  240. NodeType.ToString ());
  241. throw new XmlException (this as IXmlLineInfo, error);
  242. }
  243. if (name != Name) {
  244. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  245. Name, NamespaceURI);
  246. throw new XmlException (this as IXmlLineInfo, error);
  247. }
  248. Read ();
  249. }
  250. public virtual void ReadStartElement (string localName, string namespaceName)
  251. {
  252. if (MoveToContent () != XmlNodeType.Element) {
  253. string error = String.Format ("'{0}' is an invalid node type.",
  254. NodeType.ToString ());
  255. throw new XmlException (this as IXmlLineInfo, error);
  256. }
  257. if (localName != LocalName || NamespaceURI != namespaceName) {
  258. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  259. LocalName, NamespaceURI);
  260. throw new XmlException (this as IXmlLineInfo, error);
  261. }
  262. Read ();
  263. }
  264. public abstract string ReadString ();
  265. public abstract void ResolveEntity ();
  266. public virtual void Skip ()
  267. {
  268. if (ReadState != ReadState.Interactive)
  269. return;
  270. MoveToElement ();
  271. if (NodeType != XmlNodeType.Element || IsEmptyElement) {
  272. Read ();
  273. return;
  274. }
  275. int depth = Depth;
  276. while (Read() && depth < Depth);
  277. if (NodeType == XmlNodeType.EndElement)
  278. Read ();
  279. }
  280. #endregion
  281. }
  282. }