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