XmlElement.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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. for (int i = 0; i < ChildNodes.Count; i++)
  86. this.RemoveChild (ChildNodes [i]);
  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. XmlNameTable nt = this.OwnerDocument.NameTable;
  101. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  102. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  103. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  104. BaseURI, XmlLang, XmlSpace, null);
  105. XmlTextReader xmlReader = new XmlTextReader (value, XmlNodeType.Element, ctx);
  106. xmlReader.XmlResolver = OwnerDocument.Resolver;
  107. do {
  108. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  109. if(n == null) break;
  110. AppendChild (n);
  111. } while (true);
  112. }
  113. }
  114. public bool IsEmpty {
  115. get {
  116. return !isNotEmpty && (FirstChild == null);
  117. }
  118. set {
  119. isNotEmpty = !value;
  120. if(value) {
  121. while (FirstChild != null)
  122. RemoveChild (FirstChild);
  123. }
  124. }
  125. }
  126. public override string LocalName {
  127. get { return localName; }
  128. }
  129. public override string Name {
  130. get {
  131. if (prefix == String.Empty || prefix == null)
  132. return localName;
  133. else
  134. return prefix + ":" + localName;
  135. }
  136. }
  137. public override string NamespaceURI {
  138. get { return namespaceURI; }
  139. }
  140. // Why is this override?
  141. public override XmlNode NextSibling {
  142. get {
  143. return base.NextSibling;
  144. }
  145. }
  146. public override XmlNodeType NodeType {
  147. get {
  148. return XmlNodeType.Element;
  149. }
  150. }
  151. internal override XPathNodeType XPathNodeType {
  152. get {
  153. return XPathNodeType.Element;
  154. }
  155. }
  156. public override XmlDocument OwnerDocument {
  157. get {
  158. return base.OwnerDocument;
  159. }
  160. }
  161. public override string Prefix {
  162. get { return prefix; }
  163. set {
  164. if (IsReadOnly)
  165. throw new XmlException ("This node is readonly.");
  166. if (!XmlChar.IsNCName (value))
  167. throw new ArgumentException ("Specified name is not a valid NCName: " + value);
  168. prefix = OwnerDocument.NameTable.Add (value);
  169. }
  170. }
  171. #endregion
  172. #region Methods
  173. public override XmlNode CloneNode (bool deep)
  174. {
  175. XmlElement node = new XmlElement (
  176. prefix, localName, namespaceURI, OwnerDocument, true);
  177. for (int i = 0; i < Attributes.Count; i++)
  178. node.SetAttributeNode ((XmlAttribute)
  179. Attributes [i].CloneNode (true));
  180. if (deep) {
  181. for (int i = 0; i < ChildNodes.Count; i++)
  182. node.AppendChild (ChildNodes [i].CloneNode (true));
  183. }
  184. if (IsReadOnly)
  185. node.SetReadOnly ();
  186. return node;
  187. }
  188. public virtual string GetAttribute (string name)
  189. {
  190. XmlNode attributeNode = Attributes.GetNamedItem (name);
  191. return attributeNode != null ? attributeNode.Value : String.Empty;
  192. }
  193. public virtual string GetAttribute (string localName, string namespaceURI)
  194. {
  195. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  196. return attributeNode != null ? attributeNode.Value : String.Empty;
  197. }
  198. public virtual XmlAttribute GetAttributeNode (string name)
  199. {
  200. XmlNode attributeNode = Attributes.GetNamedItem (name);
  201. return attributeNode != null ? attributeNode as XmlAttribute : null;
  202. }
  203. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  204. {
  205. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  206. return attributeNode != null ? attributeNode as XmlAttribute : null;
  207. }
  208. public virtual XmlNodeList GetElementsByTagName (string name)
  209. {
  210. ArrayList nodeArrayList = new ArrayList ();
  211. this.SearchDescendantElements (name, name == "*", nodeArrayList);
  212. return new XmlNodeArrayList (nodeArrayList);
  213. }
  214. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  215. {
  216. ArrayList nodeArrayList = new ArrayList ();
  217. this.SearchDescendantElements (localName, localName == "*", namespaceURI, namespaceURI == "*", nodeArrayList);
  218. return new XmlNodeArrayList (nodeArrayList);
  219. }
  220. public virtual bool HasAttribute (string name)
  221. {
  222. XmlNode attributeNode = Attributes.GetNamedItem (name);
  223. return attributeNode != null;
  224. }
  225. public virtual bool HasAttribute (string localName, string namespaceURI)
  226. {
  227. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  228. return attributeNode != null;
  229. }
  230. public override void RemoveAll ()
  231. {
  232. // Remove all attributes and child nodes.
  233. base.RemoveAll ();
  234. }
  235. public virtual void RemoveAllAttributes ()
  236. {
  237. attributes.RemoveAll ();
  238. }
  239. public virtual void RemoveAttribute (string name)
  240. {
  241. XmlAttribute attr = attributes.GetNamedItem (name) as XmlAttribute;
  242. if (attr != null)
  243. attributes.Remove(attr);
  244. }
  245. public virtual void RemoveAttribute (string localName, string namespaceURI)
  246. {
  247. XmlAttribute attr = attributes.GetNamedItem(localName, namespaceURI) as XmlAttribute;
  248. if (attr != null)
  249. attributes.Remove(attr);
  250. }
  251. public virtual XmlNode RemoveAttributeAt (int i)
  252. {
  253. return attributes.RemoveAt (i);
  254. }
  255. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  256. {
  257. return attributes.Remove(oldAttr);
  258. }
  259. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  260. {
  261. return attributes.Remove(attributes[localName, namespaceURI]);
  262. }
  263. public virtual void SetAttribute (string name, string value)
  264. {
  265. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  266. attribute.Value = value;
  267. Attributes.SetNamedItem (attribute);
  268. }
  269. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  270. {
  271. XmlAttribute attr = attributes [localName, namespaceURI];
  272. if (attr == null) {
  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. XmlAttribute ret = Attributes.SetNamedItem (newAttr) as XmlAttribute;
  287. return ret == newAttr ? null : ret;
  288. }
  289. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  290. {
  291. // Note that this constraint is only for this method.
  292. // SetAttribute() allows prefixed name.
  293. XmlConvert.VerifyNCName (localName);
  294. return Attributes.Append (OwnerDocument.CreateAttribute (String.Empty, localName, namespaceURI, false, true));
  295. }
  296. public override void WriteContentTo (XmlWriter w)
  297. {
  298. int count = ChildNodes.Count;
  299. for (int i = 0; i < count; i++)
  300. ChildNodes [i].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. for (int i = 0; i < Attributes.Count; i++)
  306. if (Attributes [i].Specified)
  307. Attributes [i].WriteTo(w);
  308. if (IsEmpty)
  309. w.WriteEndElement ();
  310. else {
  311. WriteContentTo (w);
  312. w.WriteFullEndElement ();
  313. }
  314. }
  315. #endregion
  316. }
  317. }