XmlElement.cs 9.3 KB

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