XmlElement.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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. using Mono.Xml;
  17. namespace System.Xml
  18. {
  19. public class XmlElement : XmlLinkedNode
  20. {
  21. #region Fields
  22. private XmlAttributeCollection attributes;
  23. private string localName;
  24. private string namespaceURI;
  25. private string prefix;
  26. private bool isNotEmpty;
  27. #endregion
  28. #region Constructor
  29. protected internal XmlElement (
  30. string prefix,
  31. string localName,
  32. string namespaceURI,
  33. XmlDocument doc) : this (prefix, localName, namespaceURI, doc, false)
  34. {
  35. }
  36. internal XmlElement (
  37. string prefix,
  38. string localName,
  39. string namespaceURI,
  40. XmlDocument doc,
  41. bool atomizedNames) : base (doc)
  42. {
  43. if (atomizedNames) {
  44. this.prefix = prefix;
  45. this.localName = localName;
  46. this.namespaceURI = namespaceURI;
  47. } else {
  48. this.prefix = doc.NameTable.Add (prefix);
  49. this.localName = doc.NameTable.Add (localName);
  50. this.namespaceURI = doc.NameTable.Add (namespaceURI);
  51. }
  52. attributes = new XmlAttributeCollection (this);
  53. if(doc.DocumentType != null)
  54. {
  55. DTDAttListDeclaration attlist = doc.DocumentType.DTD.AttListDecls [localName];
  56. if (attlist != null) {
  57. for (int i = 0; i < attlist.Definitions.Count; i++) {
  58. DTDAttributeDefinition def = attlist [i];
  59. if (def.DefaultValue != null)
  60. SetAttribute (def.Name, def.DefaultValue);
  61. }
  62. }
  63. }
  64. }
  65. #endregion
  66. #region Properties
  67. public override XmlAttributeCollection Attributes {
  68. get { return attributes; }
  69. }
  70. public virtual bool HasAttributes {
  71. get { return attributes.Count > 0; }
  72. }
  73. public override string InnerText {
  74. get {
  75. return base.InnerText;
  76. }
  77. set {
  78. // Why its behavior (of MS FCL) is different from InnerXml...?
  79. if (FirstChild != null && FirstChild.NodeType == XmlNodeType.Text)
  80. FirstChild.Value = value;
  81. else {
  82. if(FirstChild != null) {
  83. foreach (XmlNode n in ChildNodes)
  84. this.RemoveChild (n);
  85. }
  86. // creates new Text node
  87. AppendChild(OwnerDocument.CreateTextNode(value));
  88. }
  89. }
  90. }
  91. public override string InnerXml {
  92. get {
  93. return base.InnerXml;
  94. }
  95. set {
  96. while (FirstChild != null)
  97. this.RemoveChild (FirstChild);
  98. // I hope there are any well-performance logic...
  99. XmlNameTable nt = this.OwnerDocument.NameTable;
  100. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  101. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  102. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  103. BaseURI, XmlLang, XmlSpace, null);
  104. XmlTextReader xmlReader = new XmlTextReader (value, XmlNodeType.Element, ctx);
  105. xmlReader.XmlResolver = OwnerDocument.Resolver;
  106. do {
  107. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  108. if(n == null) break;
  109. AppendChild (n);
  110. } while (true);
  111. }
  112. }
  113. public bool IsEmpty {
  114. get {
  115. return !isNotEmpty && (FirstChild == null);
  116. }
  117. set {
  118. isNotEmpty = !value;
  119. if(value) {
  120. while (FirstChild != null)
  121. RemoveChild (FirstChild);
  122. }
  123. }
  124. }
  125. public override string LocalName {
  126. get { return localName; }
  127. }
  128. public override string Name {
  129. get {
  130. if (prefix == String.Empty || prefix == null)
  131. return localName;
  132. else
  133. return prefix + ":" + localName;
  134. }
  135. }
  136. public override string NamespaceURI {
  137. get { return namespaceURI; }
  138. }
  139. // Why is this override?
  140. public override XmlNode NextSibling {
  141. get {
  142. return base.NextSibling;
  143. }
  144. }
  145. public override XmlNodeType NodeType {
  146. get {
  147. return XmlNodeType.Element;
  148. }
  149. }
  150. internal override XPathNodeType XPathNodeType {
  151. get {
  152. return XPathNodeType.Element;
  153. }
  154. }
  155. public override XmlDocument OwnerDocument {
  156. get {
  157. return base.OwnerDocument;
  158. }
  159. }
  160. public override string Prefix {
  161. get { return prefix; }
  162. set {
  163. if (IsReadOnly)
  164. throw new XmlException ("This node is readonly.");
  165. if (!XmlChar.IsNCName (value))
  166. throw new ArgumentException ("Specified name is not a valid NCName: " + value);
  167. prefix = OwnerDocument.NameTable.Add (value);
  168. }
  169. }
  170. #endregion
  171. #region Methods
  172. public override XmlNode CloneNode (bool deep)
  173. {
  174. XmlElement node = new XmlElement (
  175. prefix, localName, namespaceURI, OwnerDocument, true);
  176. for (int i = 0; i < Attributes.Count; i++)
  177. node.SetAttributeNode ((XmlAttribute)
  178. Attributes [i].CloneNode (true));
  179. if (deep) {
  180. foreach (XmlNode child in this.ChildNodes)
  181. node.AppendChild (child.CloneNode (true));
  182. } // shallow cloning
  183. return node;
  184. }
  185. public virtual string GetAttribute (string name)
  186. {
  187. XmlNode attributeNode = Attributes.GetNamedItem (name);
  188. return attributeNode != null ? attributeNode.Value : String.Empty;
  189. }
  190. public virtual string GetAttribute (string localName, string namespaceURI)
  191. {
  192. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  193. return attributeNode != null ? attributeNode.Value : String.Empty;
  194. }
  195. public virtual XmlAttribute GetAttributeNode (string name)
  196. {
  197. XmlNode attributeNode = Attributes.GetNamedItem (name);
  198. return attributeNode != null ? attributeNode as XmlAttribute : null;
  199. }
  200. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  201. {
  202. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  203. return attributeNode != null ? attributeNode as XmlAttribute : null;
  204. }
  205. public virtual XmlNodeList GetElementsByTagName (string name)
  206. {
  207. ArrayList nodeArrayList = new ArrayList ();
  208. this.SearchDescendantElements (name, name == "*", nodeArrayList);
  209. return new XmlNodeArrayList (nodeArrayList);
  210. }
  211. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  212. {
  213. ArrayList nodeArrayList = new ArrayList ();
  214. this.SearchDescendantElements (localName, localName == "*", namespaceURI, 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. public override void RemoveAll ()
  228. {
  229. // Remove the child nodes.
  230. base.RemoveAll ();
  231. // Remove all attributes.
  232. attributes.RemoveAll ();
  233. }
  234. public virtual void RemoveAllAttributes ()
  235. {
  236. attributes.RemoveAll ();
  237. }
  238. public virtual void RemoveAttribute (string name)
  239. {
  240. XmlAttribute attr = attributes.GetNamedItem (name) as XmlAttribute;
  241. if (attr != null)
  242. attributes.Remove(attr);
  243. }
  244. public virtual void RemoveAttribute (string localName, string namespaceURI)
  245. {
  246. XmlAttribute attr = attributes.GetNamedItem(localName, namespaceURI) as XmlAttribute;
  247. if (attr != null)
  248. attributes.Remove(attr);
  249. }
  250. public virtual XmlNode RemoveAttributeAt (int i)
  251. {
  252. return attributes.RemoveAt (i);
  253. }
  254. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  255. {
  256. return attributes.Remove(oldAttr);
  257. }
  258. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  259. {
  260. return attributes.Remove(attributes[localName, namespaceURI]);
  261. }
  262. public virtual void SetAttribute (string name, string value)
  263. {
  264. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  265. attribute.Value = value;
  266. Attributes.SetNamedItem (attribute);
  267. }
  268. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  269. {
  270. XmlAttribute attr = attributes[localName, namespaceURI];
  271. if(attr == null)
  272. {
  273. attr = OwnerDocument.CreateAttribute(localName, namespaceURI);
  274. attr.Value = value;
  275. attributes.SetNamedItem(attr);
  276. }
  277. else
  278. attr.Value = value;
  279. return attr.Value;
  280. }
  281. public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
  282. {
  283. if (newAttr.OwnerElement != null)
  284. throw new InvalidOperationException (
  285. "Specified attribute is already an attribute of another element.");
  286. XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
  287. return oldAttr != null ? oldAttr as XmlAttribute : null;
  288. }
  289. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  290. {
  291. XmlDocument xmlDoc = this.OwnerDocument;
  292. XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc, false);
  293. return this.attributes.Append (xmlAttribute);
  294. }
  295. public override void WriteContentTo (XmlWriter w)
  296. {
  297. foreach(XmlNode childNode in ChildNodes)
  298. childNode.WriteTo(w);
  299. }
  300. public override void WriteTo (XmlWriter w)
  301. {
  302. w.WriteStartElement(Prefix, LocalName, NamespaceURI);
  303. foreach(XmlAttribute attributeNode in Attributes)
  304. if (attributeNode.Specified)
  305. attributeNode.WriteTo(w);
  306. if (IsEmpty)
  307. w.WriteEndElement ();
  308. else {
  309. WriteContentTo(w);
  310. w.WriteFullEndElement();
  311. }
  312. }
  313. #endregion
  314. }
  315. }