XmlElement.cs 9.8 KB

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