XmlReader.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. //
  2. // XmlReader.cs
  3. //
  4. // Authors:
  5. // Jason Diamond ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Atsushi Enomoto ([email protected])
  8. //
  9. // (C) 2001, 2002 Jason Diamond http://injektilo.org/
  10. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  11. // (C) 2003 Atsushi Enomoto
  12. //
  13. using System.IO;
  14. using System.Text;
  15. namespace System.Xml
  16. {
  17. public abstract class XmlReader
  18. {
  19. private StringBuilder readStringBuffer;
  20. #region Constructor
  21. protected XmlReader ()
  22. {
  23. }
  24. #endregion
  25. #region Properties
  26. public abstract int AttributeCount { get; }
  27. public abstract string BaseURI { get; }
  28. public virtual bool CanResolveEntity
  29. {
  30. get { return false; }
  31. }
  32. public abstract int Depth { get; }
  33. public abstract bool EOF { get; }
  34. public virtual bool HasAttributes
  35. {
  36. get { return AttributeCount > 0; }
  37. }
  38. public abstract bool HasValue { get; }
  39. public abstract bool IsDefault { get; }
  40. public abstract bool IsEmptyElement { get; }
  41. public abstract string this[int i] { get; }
  42. public abstract string this[string name] { get; }
  43. public abstract string this[
  44. string localName,
  45. string namespaceName]
  46. { get; }
  47. public abstract string LocalName { get; }
  48. public abstract string Name { get; }
  49. public abstract string NamespaceURI { get; }
  50. public abstract XmlNameTable NameTable { get; }
  51. public abstract XmlNodeType NodeType { get; }
  52. public abstract string Prefix { get; }
  53. public abstract char QuoteChar { get; }
  54. public abstract ReadState ReadState { get; }
  55. public abstract string Value { get; }
  56. public abstract string XmlLang { get; }
  57. public abstract XmlSpace XmlSpace { get; }
  58. #endregion
  59. #region Methods
  60. public abstract void Close ();
  61. public abstract string GetAttribute (int i);
  62. public abstract string GetAttribute (string name);
  63. public abstract string GetAttribute (
  64. string localName,
  65. string namespaceName);
  66. public static bool IsName (string s)
  67. {
  68. bool result = false;
  69. if (s != null && s.Length > 0) {
  70. char[] chars = s.ToCharArray ();
  71. if (XmlChar.IsFirstNameChar (chars[0])) {
  72. int i = 1;
  73. int n = chars.Length;
  74. while (i < n && XmlChar.IsNameChar (chars[i]))
  75. ++i;
  76. result = i == n;
  77. }
  78. }
  79. return result;
  80. }
  81. public static bool IsNameToken (string s)
  82. {
  83. bool result = false;
  84. if (s != null && s.Length > 0) {
  85. char[] chars = s.ToCharArray ();
  86. int i = 0;
  87. int n = chars.Length;
  88. while (i < n && XmlChar.IsNameChar (chars[i]))
  89. ++i;
  90. result = i == n;
  91. }
  92. return result;
  93. }
  94. public virtual bool IsStartElement ()
  95. {
  96. return (MoveToContent () == XmlNodeType.Element);
  97. }
  98. public virtual bool IsStartElement (string name)
  99. {
  100. if (!IsStartElement ())
  101. return false;
  102. return (Name == name);
  103. }
  104. public virtual bool IsStartElement (string localName, string namespaceName)
  105. {
  106. if (!IsStartElement ())
  107. return false;
  108. return (LocalName == localName && NamespaceURI == namespaceName);
  109. }
  110. public abstract string LookupNamespace (string prefix);
  111. public abstract void MoveToAttribute (int i);
  112. public abstract bool MoveToAttribute (string name);
  113. public abstract bool MoveToAttribute (
  114. string localName,
  115. string namespaceName);
  116. private bool IsContent (XmlNodeType nodeType)
  117. {
  118. /* MS doc says:
  119. * (non-white space text, CDATA, Element, EndElement, EntityReference, or EndEntity)
  120. */
  121. switch (nodeType) {
  122. case XmlNodeType.Text:
  123. return true;
  124. case XmlNodeType.CDATA:
  125. return true;
  126. case XmlNodeType.Element:
  127. return true;
  128. case XmlNodeType.EndElement:
  129. return true;
  130. case XmlNodeType.EntityReference:
  131. return true;
  132. case XmlNodeType.EndEntity:
  133. return true;
  134. }
  135. return false;
  136. }
  137. public virtual XmlNodeType MoveToContent ()
  138. {
  139. if (NodeType == XmlNodeType.Attribute)
  140. MoveToElement ();
  141. do {
  142. if (IsContent (NodeType))
  143. return NodeType;
  144. Read ();
  145. } while (!EOF);
  146. return XmlNodeType.None;
  147. }
  148. public abstract bool MoveToElement ();
  149. public abstract bool MoveToFirstAttribute ();
  150. public abstract bool MoveToNextAttribute ();
  151. public abstract bool Read ();
  152. public abstract bool ReadAttributeValue ();
  153. public virtual string ReadElementString ()
  154. {
  155. if (MoveToContent () != XmlNodeType.Element) {
  156. string error = String.Format ("'{0}' is an invalid node type.",
  157. NodeType.ToString ());
  158. throw new XmlException (this as IXmlLineInfo, error);
  159. }
  160. string result = String.Empty;
  161. if (!IsEmptyElement) {
  162. Read ();
  163. result = ReadString ();
  164. if (NodeType != XmlNodeType.EndElement) {
  165. string error = String.Format ("'{0}' is an invalid node type.",
  166. NodeType.ToString ());
  167. throw new XmlException (this as IXmlLineInfo, error);
  168. }
  169. }
  170. Read ();
  171. return result;
  172. }
  173. public virtual string ReadElementString (string name)
  174. {
  175. if (MoveToContent () != XmlNodeType.Element) {
  176. string error = String.Format ("'{0}' is an invalid node type.",
  177. NodeType.ToString ());
  178. throw new XmlException (this as IXmlLineInfo, error);
  179. }
  180. if (name != Name) {
  181. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  182. Name, NamespaceURI);
  183. throw new XmlException (this as IXmlLineInfo, error);
  184. }
  185. string result = String.Empty;
  186. if (!IsEmptyElement) {
  187. Read ();
  188. result = ReadString ();
  189. if (NodeType != XmlNodeType.EndElement) {
  190. string error = String.Format ("'{0}' is an invalid node type.",
  191. NodeType.ToString ());
  192. throw new XmlException (this as IXmlLineInfo, error);
  193. }
  194. }
  195. Read ();
  196. return result;
  197. }
  198. public virtual string ReadElementString (string localName, string namespaceName)
  199. {
  200. if (MoveToContent () != XmlNodeType.Element) {
  201. string error = String.Format ("'{0}' is an invalid node type.",
  202. NodeType.ToString ());
  203. throw new XmlException (this as IXmlLineInfo, error);
  204. }
  205. if (localName != LocalName || NamespaceURI != namespaceName) {
  206. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  207. LocalName, NamespaceURI);
  208. throw new XmlException (this as IXmlLineInfo, error);
  209. }
  210. string result = String.Empty;
  211. if (!IsEmptyElement) {
  212. Read ();
  213. result = ReadString ();
  214. if (NodeType != XmlNodeType.EndElement) {
  215. string error = String.Format ("'{0}' is an invalid node type.",
  216. NodeType.ToString ());
  217. throw new XmlException (this as IXmlLineInfo, error);
  218. }
  219. }
  220. Read ();
  221. return result;
  222. }
  223. public virtual void ReadEndElement ()
  224. {
  225. if (MoveToContent () != XmlNodeType.EndElement) {
  226. string error = String.Format ("'{0}' is an invalid node type.",
  227. NodeType.ToString ());
  228. throw new XmlException (this as IXmlLineInfo, error);
  229. }
  230. Read ();
  231. }
  232. #if NET_1_0
  233. public abstract string ReadInnerXml ();
  234. public abstract string ReadOuterXml ();
  235. #else
  236. public virtual string ReadInnerXml ()
  237. {
  238. if (ReadState != ReadState.Interactive)
  239. return String.Empty;
  240. StringWriter sw = new StringWriter ();
  241. XmlTextWriter xtw = new XmlTextWriter (sw);
  242. if (NodeType == XmlNodeType.Element) {
  243. if (IsEmptyElement)
  244. return String.Empty;
  245. int startDepth = Depth;
  246. Read ();
  247. do {
  248. if (ReadState != ReadState.Interactive)
  249. throw new XmlException ("Unexpected end of the XML reader.");
  250. xtw.WriteNode (this, false);
  251. } while (startDepth < Depth);
  252. // reader is now end element, then proceed once more.
  253. Read ();
  254. }
  255. else
  256. xtw.WriteNode (this, false);
  257. return sw.ToString ();
  258. }
  259. public virtual string ReadOuterXml ()
  260. {
  261. if (ReadState != ReadState.Interactive)
  262. return String.Empty;
  263. StringWriter sw = new StringWriter ();
  264. XmlTextWriter xtw = new XmlTextWriter (sw);
  265. xtw.WriteNode (this, false);
  266. return sw.ToString ();
  267. }
  268. #endif
  269. public virtual void ReadStartElement ()
  270. {
  271. if (MoveToContent () != XmlNodeType.Element) {
  272. string error = String.Format ("'{0}' is an invalid node type.",
  273. NodeType.ToString ());
  274. throw new XmlException (this as IXmlLineInfo, error);
  275. }
  276. Read ();
  277. }
  278. public virtual void ReadStartElement (string name)
  279. {
  280. if (MoveToContent () != XmlNodeType.Element) {
  281. string error = String.Format ("'{0}' is an invalid node type.",
  282. NodeType.ToString ());
  283. throw new XmlException (this as IXmlLineInfo, error);
  284. }
  285. if (name != Name) {
  286. string error = String.Format ("The {0} tag from namespace {1} is expected.",
  287. Name, NamespaceURI);
  288. throw new XmlException (this as IXmlLineInfo, error);
  289. }
  290. Read ();
  291. }
  292. public virtual void ReadStartElement (string localName, string namespaceName)
  293. {
  294. if (MoveToContent () != XmlNodeType.Element) {
  295. string error = String.Format ("'{0}' is an invalid node type.",
  296. NodeType.ToString ());
  297. throw new XmlException (this as IXmlLineInfo, error);
  298. }
  299. if (localName != LocalName || NamespaceURI != namespaceName) {
  300. string error = String.Format ("Expecting {0} tag from namespace {1}, got {2} and {3} instead",
  301. localName, namespaceName,
  302. LocalName, NamespaceURI);
  303. throw new XmlException (this as IXmlLineInfo, error);
  304. }
  305. Read ();
  306. }
  307. #if NET_1_0
  308. public abstract string ReadString ();
  309. #else
  310. public virtual string ReadString ()
  311. {
  312. return ReadStringInternal ();
  313. }
  314. #endif
  315. internal string ReadStringInternal ()
  316. {
  317. if (readStringBuffer == null)
  318. readStringBuffer = new StringBuilder ();
  319. readStringBuffer.Length = 0;
  320. MoveToElement ();
  321. switch (NodeType) {
  322. default:
  323. return String.Empty;
  324. case XmlNodeType.Element:
  325. if (IsEmptyElement)
  326. return String.Empty;
  327. do {
  328. Read ();
  329. switch (NodeType) {
  330. case XmlNodeType.Text:
  331. case XmlNodeType.CDATA:
  332. case XmlNodeType.Whitespace:
  333. case XmlNodeType.SignificantWhitespace:
  334. readStringBuffer.Append (Value);
  335. continue;
  336. }
  337. break;
  338. } while (true);
  339. break;
  340. case XmlNodeType.Text:
  341. case XmlNodeType.CDATA:
  342. case XmlNodeType.Whitespace:
  343. case XmlNodeType.SignificantWhitespace:
  344. do {
  345. switch (NodeType) {
  346. case XmlNodeType.Text:
  347. case XmlNodeType.CDATA:
  348. case XmlNodeType.Whitespace:
  349. case XmlNodeType.SignificantWhitespace:
  350. readStringBuffer.Append (Value);
  351. Read ();
  352. continue;
  353. }
  354. break;
  355. } while (true);
  356. break;
  357. }
  358. string ret = readStringBuffer.ToString ();
  359. readStringBuffer.Length = 0;
  360. return ret;
  361. }
  362. public abstract void ResolveEntity ();
  363. public virtual void Skip ()
  364. {
  365. if (ReadState != ReadState.Interactive)
  366. return;
  367. MoveToElement ();
  368. if (NodeType != XmlNodeType.Element || IsEmptyElement) {
  369. Read ();
  370. return;
  371. }
  372. int depth = Depth;
  373. while (Read() && depth < Depth);
  374. if (NodeType == XmlNodeType.EndElement)
  375. Read ();
  376. }
  377. #endregion
  378. }
  379. }