XmlElement.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // System.Xml.XmlElement
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) 2002 Jason Diamond http://injektilo.org/
  9. // (C) 2002 Atsushi Enomoto
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Xml.XPath;
  14. using System.IO;
  15. using System.Text;
  16. namespace System.Xml
  17. {
  18. public class XmlElement : XmlLinkedNode
  19. {
  20. #region Fields
  21. private XmlAttributeCollection attributes;
  22. private string localName;
  23. private string namespaceURI;
  24. private string prefix;
  25. private bool isEmpty;
  26. #endregion
  27. #region Constructor
  28. protected internal XmlElement (
  29. string prefix,
  30. string localName,
  31. string namespaceURI,
  32. XmlDocument doc) : base (doc)
  33. {
  34. this.prefix = prefix;
  35. this.localName = localName;
  36. this.namespaceURI = namespaceURI;
  37. attributes = new XmlAttributeCollection (this);
  38. // TODO: Adds default attributes
  39. if(doc.DocumentType != null)
  40. {
  41. }
  42. }
  43. #endregion
  44. #region Properties
  45. public override XmlAttributeCollection Attributes {
  46. get { return attributes; }
  47. }
  48. public virtual bool HasAttributes {
  49. get { return attributes.Count > 0; }
  50. }
  51. public override string InnerText {
  52. get {
  53. return base.InnerText;
  54. }
  55. set {
  56. foreach(XmlNode n in ChildNodes)
  57. {
  58. this.RemoveChild(n);
  59. }
  60. // creates new Text node
  61. AppendChild(OwnerDocument.CreateTextNode(value));
  62. }
  63. }
  64. public override string InnerXml {
  65. get {
  66. return base.InnerXml;
  67. }
  68. set {
  69. foreach(XmlNode n in ChildNodes)
  70. this.RemoveChild(n);
  71. // I hope there are any well-performance logic...
  72. XmlNameTable nt = this.OwnerDocument.NameTable;
  73. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  74. XmlParserContext ctx = new XmlParserContext (nt, nsmgr, XmlLang, this.XmlSpace);
  75. XmlTextReader xmlReader = OwnerDocument.ReusableReader;
  76. xmlReader.SetReaderContext (String.Empty, ctx);
  77. xmlReader.SetReaderFragment (new StringReader (value), XmlNodeType.Element);
  78. do {
  79. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  80. if(n == null) break;
  81. AppendChild (n);
  82. } while (true);
  83. }
  84. }
  85. public bool IsEmpty {
  86. get { return isEmpty; }
  87. set {
  88. if(value)
  89. RemoveAll();
  90. isEmpty = value;
  91. }
  92. }
  93. public override string LocalName {
  94. get { return localName; }
  95. }
  96. public override string Name {
  97. get {
  98. return prefix != String.Empty ? prefix + ":" + localName : localName;
  99. }
  100. }
  101. public override string NamespaceURI {
  102. get { return namespaceURI; }
  103. }
  104. [MonoTODO]
  105. public override XmlNode NextSibling {
  106. get {
  107. return base.NextSibling;
  108. }
  109. }
  110. public override XmlNodeType NodeType {
  111. get {
  112. return XmlNodeType.Element;
  113. }
  114. }
  115. internal override XPathNodeType XPathNodeType {
  116. get {
  117. return XPathNodeType.Element;
  118. }
  119. }
  120. [MonoTODO]
  121. public override XmlDocument OwnerDocument {
  122. get {
  123. return base.OwnerDocument;
  124. }
  125. }
  126. public override string Prefix {
  127. get { return prefix; }
  128. set { prefix = value; }
  129. }
  130. #endregion
  131. #region Methods
  132. [MonoTODO]
  133. public override XmlNode CloneNode (bool deep)
  134. {
  135. XmlNode node = new XmlElement (prefix, localName, namespaceURI,
  136. OwnerDocument);
  137. for (int i = 0; i < node.Attributes.Count; i++)
  138. node.AppendChild (node.Attributes [i].CloneNode (false));
  139. if (deep) {
  140. while ((node != null) && (node.HasChildNodes)) {
  141. AppendChild (node.NextSibling.CloneNode (true));
  142. node = node.NextSibling;
  143. }
  144. } // shallow cloning
  145. //
  146. // Reminder: Also look into Default attributes.
  147. //
  148. return node;
  149. }
  150. [MonoTODO]
  151. public virtual string GetAttribute (string name)
  152. {
  153. XmlNode attributeNode = Attributes.GetNamedItem (name);
  154. return attributeNode != null ? attributeNode.Value : String.Empty;
  155. }
  156. [MonoTODO]
  157. public virtual string GetAttribute (string localName, string namespaceURI)
  158. {
  159. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  160. return attributeNode != null ? attributeNode.Value : String.Empty;
  161. }
  162. [MonoTODO]
  163. public virtual XmlAttribute GetAttributeNode (string name)
  164. {
  165. XmlNode attributeNode = Attributes.GetNamedItem (name);
  166. return attributeNode != null ? attributeNode as XmlAttribute : null;
  167. }
  168. [MonoTODO]
  169. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  170. {
  171. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  172. return attributeNode != null ? attributeNode as XmlAttribute : null;
  173. }
  174. public virtual XmlNodeList GetElementsByTagName (string name)
  175. {
  176. ArrayList nodeArrayList = new ArrayList ();
  177. this.searchNodesRecursively (this, name, nodeArrayList);
  178. return new XmlNodeArrayList (nodeArrayList);
  179. }
  180. private void searchNodesRecursively (XmlNode argNode, string argName,
  181. ArrayList argArrayList)
  182. {
  183. XmlNodeList xmlNodeList = argNode.ChildNodes;
  184. foreach (XmlNode node in xmlNodeList){
  185. if (node.Name.Equals (argName))
  186. argArrayList.Add (node);
  187. else
  188. this.searchNodesRecursively (node, argName, argArrayList);
  189. }
  190. }
  191. private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
  192. ArrayList argArrayList)
  193. {
  194. XmlNodeList xmlNodeList = argNode.ChildNodes;
  195. foreach (XmlNode node in xmlNodeList)
  196. {
  197. if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
  198. argArrayList.Add (node);
  199. else
  200. this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
  201. }
  202. }
  203. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  204. {
  205. ArrayList nodeArrayList = new ArrayList ();
  206. this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
  207. return new XmlNodeArrayList (nodeArrayList);
  208. }
  209. [MonoTODO]
  210. public virtual bool HasAttribute (string name)
  211. {
  212. XmlNode attributeNode = Attributes.GetNamedItem (name);
  213. return attributeNode != null;
  214. }
  215. [MonoTODO]
  216. public virtual bool HasAttribute (string localName, string namespaceURI)
  217. {
  218. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  219. return attributeNode != null;
  220. }
  221. [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
  222. public override void RemoveAll ()
  223. {
  224. // Remove the child nodes.
  225. base.RemoveAll ();
  226. // Remove all attributes.
  227. attributes.RemoveAll ();
  228. }
  229. [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
  230. public virtual void RemoveAllAttributes ()
  231. {
  232. attributes.RemoveAll ();
  233. }
  234. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  235. public virtual void RemoveAttribute (string name)
  236. {
  237. attributes.Remove((XmlAttribute)attributes.GetNamedItem(name));
  238. }
  239. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  240. public virtual void RemoveAttribute (string localName, string namespaceURI)
  241. {
  242. attributes.Remove((XmlAttribute)attributes.GetNamedItem(localName, namespaceURI));
  243. }
  244. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  245. public virtual XmlNode RemoveAttributeAt (int i)
  246. {
  247. return attributes.Remove(attributes[i]);
  248. }
  249. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  250. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  251. {
  252. return attributes.Remove(oldAttr);
  253. }
  254. [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
  255. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  256. {
  257. return attributes.Remove(attributes[localName, namespaceURI]);
  258. }
  259. [MonoTODO]
  260. public virtual void SetAttribute (string name, string value)
  261. {
  262. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  263. attribute.SetOwnerElement(this);
  264. attribute.Value = value;
  265. Attributes.SetNamedItem (attribute);
  266. }
  267. // [MonoTODO]
  268. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  269. {
  270. XmlAttribute attr = attributes[localName, namespaceURI];
  271. if(attr == null)
  272. {
  273. attr = OwnerDocument.CreateAttribute(localName, namespaceURI);
  274. attr.Value = value;
  275. attributes.SetNamedItem(attr);
  276. }
  277. else
  278. attr.Value = value;
  279. return attr.Value;
  280. }
  281. // [MonoTODO]
  282. public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
  283. {
  284. newAttr.SetOwnerElement(this);
  285. XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
  286. return oldAttr != null ? oldAttr as XmlAttribute : null;
  287. }
  288. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  289. {
  290. XmlDocument xmlDoc = this.OwnerDocument;
  291. XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc);
  292. return this.attributes.Append (xmlAttribute);
  293. }
  294. public override void WriteContentTo (XmlWriter w)
  295. {
  296. foreach(XmlNode childNode in ChildNodes)
  297. childNode.WriteTo(w);
  298. }
  299. [MonoTODO]
  300. public override void WriteTo (XmlWriter w)
  301. {
  302. w.WriteStartElement(Prefix, LocalName, NamespaceURI);
  303. // write namespace declarations(if not exist)
  304. if(Prefix != null && w.LookupPrefix (Prefix) != NamespaceURI)
  305. w.WriteAttributeString ("xmlns", Prefix, "http://www.w3.org/2000/xmlns/", NamespaceURI);
  306. foreach(XmlNode attributeNode in Attributes)
  307. {
  308. attributeNode.WriteTo(w);
  309. // write namespace declarations(if not exist)
  310. if(attributeNode.Prefix != null &&
  311. w.LookupPrefix(attributeNode.Prefix) != attributeNode.NamespaceURI &&
  312. attributeNode.Prefix != "xmlns")
  313. w.WriteAttributeString("xmlns", attributeNode.Prefix, "http://www.w3.org/2000/xmlns/", attributeNode.NamespaceURI);
  314. }
  315. WriteContentTo(w);
  316. w.WriteEndElement();
  317. }
  318. #endregion
  319. }
  320. }