XmlElement.cs 9.6 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. 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) : base (doc)
  34. {
  35. this.prefix = doc.NameTable.Add (prefix);
  36. this.localName = doc.NameTable.Add (localName);
  37. this.namespaceURI = doc.NameTable.Add (namespaceURI);
  38. attributes = new XmlAttributeCollection (this);
  39. if(doc.DocumentType != null)
  40. {
  41. DTDAttListDeclaration attlist = doc.DocumentType.DTD.AttListDecls [localName];
  42. if (attlist != null) {
  43. for (int i = 0; i < attlist.Definitions.Count; i++) {
  44. DTDAttributeDefinition def = attlist [i];
  45. if (def.DefaultValue != null)
  46. SetAttribute (def.Name, def.DefaultValue);
  47. }
  48. }
  49. }
  50. }
  51. #endregion
  52. #region Properties
  53. public override XmlAttributeCollection Attributes {
  54. get { return attributes; }
  55. }
  56. public virtual bool HasAttributes {
  57. get { return attributes.Count > 0; }
  58. }
  59. public override string InnerText {
  60. get {
  61. return base.InnerText;
  62. }
  63. set {
  64. // Why its behavior (of MS FCL) is different from InnerXml...?
  65. if (FirstChild != null && FirstChild.NodeType == XmlNodeType.Text)
  66. FirstChild.Value = value;
  67. else {
  68. if(FirstChild != null) {
  69. foreach (XmlNode n in ChildNodes)
  70. this.RemoveChild (n);
  71. }
  72. // creates new Text node
  73. AppendChild(OwnerDocument.CreateTextNode(value));
  74. }
  75. }
  76. }
  77. public override string InnerXml {
  78. get {
  79. return base.InnerXml;
  80. }
  81. set {
  82. while (FirstChild != null)
  83. this.RemoveChild (FirstChild);
  84. // I hope there are any well-performance logic...
  85. XmlNameTable nt = this.OwnerDocument.NameTable;
  86. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  87. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  88. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  89. BaseURI, XmlLang, XmlSpace, null);
  90. XmlTextReader xmlReader = new XmlTextReader (value, XmlNodeType.Element, ctx);
  91. xmlReader.XmlResolver = OwnerDocument.Resolver;
  92. do {
  93. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  94. if(n == null) break;
  95. AppendChild (n);
  96. } while (true);
  97. }
  98. }
  99. public bool IsEmpty {
  100. get {
  101. return !isNotEmpty && (FirstChild == null);
  102. }
  103. set {
  104. isNotEmpty = !value;
  105. if(value) {
  106. while (FirstChild != null)
  107. RemoveChild (FirstChild);
  108. }
  109. }
  110. }
  111. public override string LocalName {
  112. get { return localName; }
  113. }
  114. public override string Name {
  115. get {
  116. if (prefix == String.Empty || prefix == null)
  117. return localName;
  118. else
  119. return prefix + ":" + localName;
  120. }
  121. }
  122. public override string NamespaceURI {
  123. get { return namespaceURI; }
  124. }
  125. // Why is this override?
  126. public override XmlNode NextSibling {
  127. get {
  128. return base.NextSibling;
  129. }
  130. }
  131. public override XmlNodeType NodeType {
  132. get {
  133. return XmlNodeType.Element;
  134. }
  135. }
  136. internal override XPathNodeType XPathNodeType {
  137. get {
  138. return XPathNodeType.Element;
  139. }
  140. }
  141. public override XmlDocument OwnerDocument {
  142. get {
  143. return base.OwnerDocument;
  144. }
  145. }
  146. public override string Prefix {
  147. get { return prefix; }
  148. set {
  149. if (IsReadOnly)
  150. throw new XmlException ("This node is readonly.");
  151. if (!XmlChar.IsNCName (value))
  152. throw new ArgumentException ("Specified name is not a valid NCName: " + value);
  153. prefix = value;
  154. }
  155. }
  156. #endregion
  157. #region Methods
  158. public override XmlNode CloneNode (bool deep)
  159. {
  160. XmlElement node = new XmlElement (
  161. prefix, localName, namespaceURI, OwnerDocument);
  162. for (int i = 0; i < Attributes.Count; i++)
  163. node.SetAttributeNode ((XmlAttribute)
  164. Attributes [i].CloneNode (true));
  165. if (deep) {
  166. foreach (XmlNode child in this.ChildNodes)
  167. node.AppendChild (child.CloneNode (true));
  168. } // shallow cloning
  169. return node;
  170. }
  171. public virtual string GetAttribute (string name)
  172. {
  173. XmlNode attributeNode = Attributes.GetNamedItem (name);
  174. return attributeNode != null ? attributeNode.Value : String.Empty;
  175. }
  176. public virtual string GetAttribute (string localName, string namespaceURI)
  177. {
  178. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  179. return attributeNode != null ? attributeNode.Value : String.Empty;
  180. }
  181. public virtual XmlAttribute GetAttributeNode (string name)
  182. {
  183. XmlNode attributeNode = Attributes.GetNamedItem (name);
  184. return attributeNode != null ? attributeNode as XmlAttribute : null;
  185. }
  186. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  187. {
  188. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  189. return attributeNode != null ? attributeNode as XmlAttribute : null;
  190. }
  191. public virtual XmlNodeList GetElementsByTagName (string name)
  192. {
  193. ArrayList nodeArrayList = new ArrayList ();
  194. this.searchNodesRecursively (this, name, nodeArrayList);
  195. return new XmlNodeArrayList (nodeArrayList);
  196. }
  197. private void searchNodesRecursively (XmlNode argNode, string argName,
  198. ArrayList argArrayList)
  199. {
  200. XmlNodeList xmlNodeList = argNode.ChildNodes;
  201. foreach (XmlNode node in xmlNodeList){
  202. if (node.Name.Equals (argName))
  203. argArrayList.Add (node);
  204. else
  205. this.searchNodesRecursively (node, argName, argArrayList);
  206. }
  207. }
  208. private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
  209. ArrayList argArrayList)
  210. {
  211. XmlNodeList xmlNodeList = argNode.ChildNodes;
  212. foreach (XmlNode node in xmlNodeList)
  213. {
  214. if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
  215. argArrayList.Add (node);
  216. else
  217. this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
  218. }
  219. }
  220. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  221. {
  222. ArrayList nodeArrayList = new ArrayList ();
  223. this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
  224. return new XmlNodeArrayList (nodeArrayList);
  225. }
  226. public virtual bool HasAttribute (string name)
  227. {
  228. XmlNode attributeNode = Attributes.GetNamedItem (name);
  229. return attributeNode != null;
  230. }
  231. public virtual bool HasAttribute (string localName, string namespaceURI)
  232. {
  233. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  234. return attributeNode != null;
  235. }
  236. public override void RemoveAll ()
  237. {
  238. // Remove the child nodes.
  239. base.RemoveAll ();
  240. // Remove all attributes.
  241. attributes.RemoveAll ();
  242. }
  243. public virtual void RemoveAllAttributes ()
  244. {
  245. attributes.RemoveAll ();
  246. }
  247. public virtual void RemoveAttribute (string name)
  248. {
  249. XmlAttribute attr = attributes.GetNamedItem (name) as XmlAttribute;
  250. if (attr != null)
  251. attributes.Remove(attr);
  252. }
  253. public virtual void RemoveAttribute (string localName, string namespaceURI)
  254. {
  255. XmlAttribute attr = attributes.GetNamedItem(localName, namespaceURI) as XmlAttribute;
  256. if (attr != null)
  257. attributes.Remove(attr);
  258. }
  259. public virtual XmlNode RemoveAttributeAt (int i)
  260. {
  261. return attributes.RemoveAt (i);
  262. }
  263. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  264. {
  265. return attributes.Remove(oldAttr);
  266. }
  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. }