XmlElement.cs 9.8 KB

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