XmlElement.cs 9.8 KB

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