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