XmlElement.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. //
  2. // System.Xml.XmlAttribute
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. //
  7. // (C) 2002 Jason Diamond http://injektilo.org/
  8. //
  9. using System;
  10. using System.Collections;
  11. namespace System.Xml
  12. {
  13. public class XmlElement : XmlLinkedNode
  14. {
  15. #region Fields
  16. private XmlAttributeCollection attributes;
  17. private XmlLinkedNode lastLinkedChild;
  18. private string localName;
  19. private string namespaceURI;
  20. private string prefix;
  21. #endregion
  22. #region Constructor
  23. protected internal XmlElement (
  24. string prefix,
  25. string localName,
  26. string namespaceURI,
  27. XmlDocument doc) : base (doc)
  28. {
  29. this.prefix = prefix;
  30. this.localName = localName;
  31. this.namespaceURI = namespaceURI;
  32. attributes = new XmlAttributeCollection (this);
  33. }
  34. #endregion
  35. #region Properties
  36. public override XmlAttributeCollection Attributes {
  37. get { return attributes; }
  38. }
  39. public virtual bool HasAttributes {
  40. get { return attributes.Count > 0; }
  41. }
  42. [MonoTODO ("Setter.")]
  43. public override string InnerXml {
  44. get {
  45. // Not sure why this is an override. Passing through for now.
  46. return base.InnerXml;
  47. }
  48. set { throw new NotImplementedException (); }
  49. }
  50. [MonoTODO]
  51. public bool IsEmpty {
  52. get { throw new NotImplementedException (); }
  53. set { throw new NotImplementedException (); }
  54. }
  55. internal override XmlLinkedNode LastLinkedChild {
  56. get { return lastLinkedChild; }
  57. set { lastLinkedChild = value; }
  58. }
  59. public override string LocalName {
  60. get { return localName; }
  61. }
  62. public override string Name {
  63. get {
  64. return prefix != String.Empty ? prefix + ":" + localName : localName;
  65. }
  66. }
  67. public override string NamespaceURI {
  68. get { return namespaceURI; }
  69. }
  70. [MonoTODO]
  71. public override XmlNode NextSibling {
  72. get {
  73. return base.NextSibling;
  74. }
  75. }
  76. public override XmlNodeType NodeType {
  77. get {
  78. return XmlNodeType.Element;
  79. }
  80. }
  81. [MonoTODO]
  82. public override XmlDocument OwnerDocument {
  83. get {
  84. return base.OwnerDocument;
  85. }
  86. }
  87. public override string Prefix {
  88. get {
  89. return prefix;
  90. }
  91. }
  92. #endregion
  93. #region Methods
  94. [MonoTODO]
  95. public override XmlNode CloneNode (bool deep)
  96. {
  97. XmlNode node = new XmlElement (prefix, localName, namespaceURI,
  98. OwnerDocument);
  99. for (int i = 0; i < node.Attributes.Count; i++)
  100. node.AppendChild (node.Attributes [i].CloneNode (false));
  101. if (deep) {
  102. while ((node != null) && (node.HasChildNodes)) {
  103. AppendChild (node.NextSibling.CloneNode (true));
  104. node = node.NextSibling;
  105. }
  106. } // shallow cloning
  107. //
  108. // Reminder: Also look into Default attributes.
  109. //
  110. return node;
  111. }
  112. [MonoTODO]
  113. public virtual string GetAttribute (string name)
  114. {
  115. XmlNode attributeNode = Attributes.GetNamedItem (name);
  116. return attributeNode != null ? attributeNode.Value : String.Empty;
  117. }
  118. [MonoTODO]
  119. public virtual string GetAttribute (string localName, string namespaceURI)
  120. {
  121. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  122. return attributeNode != null ? attributeNode.Value : String.Empty;
  123. }
  124. [MonoTODO]
  125. public virtual XmlAttribute GetAttributeNode (string name)
  126. {
  127. XmlNode attributeNode = Attributes.GetNamedItem (name);
  128. return attributeNode != null ? attributeNode as XmlAttribute : null;
  129. }
  130. [MonoTODO]
  131. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  132. {
  133. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  134. return attributeNode != null ? attributeNode as XmlAttribute : null;
  135. }
  136. public virtual XmlNodeList GetElementsByTagName (string name)
  137. {
  138. ArrayList nodeArrayList = new ArrayList ();
  139. this.searchNodesRecursively (this, name, nodeArrayList);
  140. return new XmlNodeArrayList (nodeArrayList);
  141. }
  142. private void searchNodesRecursively (XmlNode argNode, string argName, ArrayList argArrayList)
  143. {
  144. XmlNodeList xmlNodeList = argNode.ChildNodes;
  145. foreach (XmlNode node in xmlNodeList){
  146. if (node.Name.Equals (argName))
  147. argArrayList.Add (node);
  148. else
  149. this.searchNodesRecursively (node, argName, argArrayList);
  150. }
  151. }
  152. [MonoTODO]
  153. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  154. {
  155. throw new NotImplementedException ();
  156. }
  157. [MonoTODO]
  158. public virtual bool HasAttribute (string name)
  159. {
  160. XmlNode attributeNode = Attributes.GetNamedItem (name);
  161. return attributeNode != null;
  162. }
  163. [MonoTODO]
  164. public virtual bool HasAttribute (string localName, string namespaceURI)
  165. {
  166. throw new NotImplementedException ();
  167. }
  168. [MonoTODO ("Don't remove default attributes.")]
  169. public override void RemoveAll ()
  170. {
  171. // Remove the child nodes.
  172. base.RemoveAll ();
  173. // Remove all attributes.
  174. attributes.RemoveAll ();
  175. }
  176. [MonoTODO]
  177. public virtual void RemoveAllAttributes ()
  178. {
  179. throw new NotImplementedException ();
  180. }
  181. [MonoTODO]
  182. public virtual void RemoveAttribute (string name)
  183. {
  184. throw new NotImplementedException ();
  185. }
  186. [MonoTODO]
  187. public virtual void RemoveAttribute (string localName, string namespaceURI)
  188. {
  189. throw new NotImplementedException ();
  190. }
  191. [MonoTODO]
  192. public virtual XmlNode RemoveAttributeAt (int i)
  193. {
  194. throw new NotImplementedException ();
  195. }
  196. [MonoTODO]
  197. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  198. {
  199. throw new NotImplementedException ();
  200. }
  201. [MonoTODO]
  202. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  203. {
  204. throw new NotImplementedException ();
  205. }
  206. [MonoTODO]
  207. public virtual void SetAttribute (string name, string value)
  208. {
  209. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  210. attribute.SetParentNode (this);
  211. attribute.Value = value;
  212. Attributes.SetNamedItem (attribute);
  213. }
  214. [MonoTODO]
  215. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  216. {
  217. throw new NotImplementedException ();
  218. }
  219. [MonoTODO]
  220. public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
  221. {
  222. newAttr.SetParentNode(this);
  223. XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
  224. return oldAttr != null ? oldAttr as XmlAttribute : null;
  225. }
  226. [MonoTODO]
  227. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  228. {
  229. throw new NotImplementedException ();
  230. }
  231. public override void WriteContentTo (XmlWriter w)
  232. {
  233. foreach(XmlNode childNode in ChildNodes)
  234. childNode.WriteTo(w);
  235. }
  236. public override void WriteTo (XmlWriter w)
  237. {
  238. w.WriteStartElement(Prefix, LocalName, NamespaceURI);
  239. foreach(XmlNode attributeNode in Attributes)
  240. attributeNode.WriteTo(w);
  241. WriteContentTo(w);
  242. w.WriteEndElement();
  243. }
  244. #endregion
  245. }
  246. }