XmlElement.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Xml.XPath;
  34. using System.IO;
  35. using System.Text;
  36. using Mono.Xml;
  37. #if NET_2_0
  38. using System.Xml.Schema;
  39. #endif
  40. namespace System.Xml
  41. {
  42. public class XmlElement : XmlLinkedNode
  43. {
  44. #region Fields
  45. private XmlAttributeCollection attributes;
  46. private XmlNameEntry name;
  47. private bool isNotEmpty;
  48. #if NET_2_0
  49. IXmlSchemaInfo schemaInfo;
  50. #endif
  51. #endregion
  52. #region Constructor
  53. protected internal XmlElement (
  54. string prefix,
  55. string localName,
  56. string namespaceURI,
  57. XmlDocument doc) : this (prefix, localName, namespaceURI, doc, false)
  58. {
  59. }
  60. internal XmlElement (
  61. string prefix,
  62. string localName,
  63. string namespaceURI,
  64. XmlDocument doc,
  65. bool atomizedNames) : base (doc)
  66. {
  67. XmlConvert.VerifyName (localName);
  68. if (!atomizedNames) {
  69. prefix = doc.NameTable.Add (prefix);
  70. localName = doc.NameTable.Add (localName);
  71. namespaceURI = doc.NameTable.Add (namespaceURI);
  72. }
  73. name = doc.NameCache.Add (prefix, localName, namespaceURI, true);
  74. // attributes = new XmlAttributeCollection (this);
  75. if(doc.DocumentType != null)
  76. {
  77. DTDAttListDeclaration attlist = doc.DocumentType.DTD.AttListDecls [localName];
  78. if (attlist != null) {
  79. for (int i = 0; i < attlist.Definitions.Count; i++) {
  80. DTDAttributeDefinition def = attlist [i];
  81. if (def.DefaultValue != null) {
  82. SetAttribute (def.Name, def.DefaultValue);
  83. Attributes [def.Name].SetDefault ();
  84. }
  85. }
  86. }
  87. }
  88. }
  89. #endregion
  90. #region Properties
  91. public override XmlAttributeCollection Attributes {
  92. get {
  93. if (attributes == null)
  94. attributes = new XmlAttributeCollection (this);
  95. return attributes;
  96. }
  97. }
  98. public virtual bool HasAttributes {
  99. get { return attributes != null && attributes.Count > 0; }
  100. }
  101. public override string InnerText {
  102. get {
  103. return base.InnerText;
  104. }
  105. set {
  106. // Why its behavior (of MS FCL) is different from InnerXml...?
  107. if (ChildNodes != null && ChildNodes.Count == 1 && FirstChild.NodeType == XmlNodeType.Text)
  108. FirstChild.Value = value;
  109. else {
  110. while (FirstChild != null)
  111. this.RemoveChild (FirstChild);
  112. // creates new Text node
  113. AppendChild (OwnerDocument.CreateTextNode (value));
  114. }
  115. }
  116. }
  117. public override string InnerXml {
  118. get {
  119. return base.InnerXml;
  120. }
  121. set {
  122. while (FirstChild != null)
  123. this.RemoveChild (FirstChild);
  124. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  125. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  126. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  127. BaseURI, XmlLang, XmlSpace, null);
  128. XmlTextReader xmlReader = new XmlTextReader (value, XmlNodeType.Element, ctx);
  129. xmlReader.XmlResolver = OwnerDocument.Resolver;
  130. do {
  131. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  132. if(n == null) break;
  133. AppendChild (n);
  134. } while (true);
  135. }
  136. }
  137. public bool IsEmpty {
  138. get {
  139. return !isNotEmpty && (FirstChild == null);
  140. }
  141. set {
  142. isNotEmpty = !value;
  143. if(value) {
  144. while (FirstChild != null)
  145. RemoveChild (FirstChild);
  146. }
  147. }
  148. }
  149. public override string LocalName {
  150. get { return name.LocalName; }
  151. }
  152. public override string Name {
  153. get {
  154. if (name.Prefix == String.Empty || name.Prefix == null)
  155. return name.LocalName;
  156. else
  157. return OwnerDocument.NameTable.Add (name.Prefix + ":" + name.LocalName);
  158. }
  159. }
  160. public override string NamespaceURI {
  161. get { return name.NS; }
  162. }
  163. // Why is this override?
  164. public override XmlNode NextSibling {
  165. get {
  166. return base.NextSibling;
  167. }
  168. }
  169. public override XmlNodeType NodeType {
  170. get {
  171. return XmlNodeType.Element;
  172. }
  173. }
  174. internal override XPathNodeType XPathNodeType {
  175. get {
  176. return XPathNodeType.Element;
  177. }
  178. }
  179. public override XmlDocument OwnerDocument {
  180. get {
  181. return base.OwnerDocument;
  182. }
  183. }
  184. public override string Prefix {
  185. get { return name.Prefix; }
  186. set {
  187. if (IsReadOnly)
  188. throw new XmlException ("This node is readonly.");
  189. if (!XmlChar.IsNCName (value))
  190. throw new ArgumentException ("Specified name is not a valid NCName: " + value);
  191. value = OwnerDocument.NameTable.Add (value);
  192. name = OwnerDocument.NameCache.Add (value,
  193. name.LocalName, name.NS, true);
  194. }
  195. }
  196. #if NET_2_0
  197. [CLSCompliant(false)]
  198. public override IXmlSchemaInfo SchemaInfo {
  199. get { return schemaInfo; }
  200. internal set { schemaInfo = value; }
  201. }
  202. #endif
  203. #endregion
  204. #region Methods
  205. public override XmlNode CloneNode (bool deep)
  206. {
  207. XmlElement node = new XmlElement (
  208. name.Prefix, name.LocalName, name.NS, OwnerDocument, true);
  209. for (int i = 0; i < Attributes.Count; i++)
  210. node.SetAttributeNode ((XmlAttribute)
  211. Attributes [i].CloneNode (true));
  212. if (deep) {
  213. for (int i = 0; i < ChildNodes.Count; i++)
  214. node.AppendChild (ChildNodes [i].CloneNode (true));
  215. }
  216. return node;
  217. }
  218. public virtual string GetAttribute (string name)
  219. {
  220. XmlNode attributeNode = Attributes.GetNamedItem (name);
  221. return attributeNode != null ? attributeNode.Value : String.Empty;
  222. }
  223. public virtual string GetAttribute (string localName, string namespaceURI)
  224. {
  225. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  226. return attributeNode != null ? attributeNode.Value : String.Empty;
  227. }
  228. public virtual XmlAttribute GetAttributeNode (string name)
  229. {
  230. XmlNode attributeNode = Attributes.GetNamedItem (name);
  231. return attributeNode != null ? attributeNode as XmlAttribute : null;
  232. }
  233. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  234. {
  235. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  236. return attributeNode != null ? attributeNode as XmlAttribute : null;
  237. }
  238. public virtual XmlNodeList GetElementsByTagName (string name)
  239. {
  240. ArrayList nodeArrayList = new ArrayList ();
  241. this.SearchDescendantElements (name, name == "*", nodeArrayList);
  242. return new XmlNodeArrayList (nodeArrayList);
  243. }
  244. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  245. {
  246. ArrayList nodeArrayList = new ArrayList ();
  247. this.SearchDescendantElements (localName, localName == "*", namespaceURI, namespaceURI == "*", nodeArrayList);
  248. return new XmlNodeArrayList (nodeArrayList);
  249. }
  250. public virtual bool HasAttribute (string name)
  251. {
  252. XmlNode attributeNode = Attributes.GetNamedItem (name);
  253. return attributeNode != null;
  254. }
  255. public virtual bool HasAttribute (string localName, string namespaceURI)
  256. {
  257. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  258. return attributeNode != null;
  259. }
  260. public override void RemoveAll ()
  261. {
  262. // Remove all attributes and child nodes.
  263. base.RemoveAll ();
  264. }
  265. public virtual void RemoveAllAttributes ()
  266. {
  267. if (attributes != null)
  268. attributes.RemoveAll ();
  269. }
  270. public virtual void RemoveAttribute (string name)
  271. {
  272. if (attributes == null)
  273. return;
  274. XmlAttribute attr = Attributes.GetNamedItem (name) as XmlAttribute;
  275. if (attr != null)
  276. Attributes.Remove(attr);
  277. }
  278. public virtual void RemoveAttribute (string localName, string namespaceURI)
  279. {
  280. if (attributes == null)
  281. return;
  282. XmlAttribute attr = attributes.GetNamedItem(localName, namespaceURI) as XmlAttribute;
  283. if (attr != null)
  284. Attributes.Remove(attr);
  285. }
  286. public virtual XmlNode RemoveAttributeAt (int i)
  287. {
  288. if (attributes == null || attributes.Count <= i)
  289. return null;
  290. return Attributes.RemoveAt (i);
  291. }
  292. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  293. {
  294. if (attributes == null)
  295. return null;
  296. return Attributes.Remove (oldAttr);
  297. }
  298. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  299. {
  300. if (attributes == null)
  301. return null;
  302. return Attributes.Remove (attributes [localName, namespaceURI]);
  303. }
  304. public virtual void SetAttribute (string name, string value)
  305. {
  306. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  307. attribute.Value = value;
  308. Attributes.SetNamedItem (attribute);
  309. }
  310. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  311. {
  312. XmlAttribute attr = Attributes [localName, namespaceURI];
  313. if (attr == null) {
  314. attr = OwnerDocument.CreateAttribute (localName, namespaceURI);
  315. attr.Value = value;
  316. Attributes.SetNamedItem (attr);
  317. }
  318. else
  319. attr.Value = value;
  320. return attr.Value;
  321. }
  322. public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
  323. {
  324. if (newAttr.OwnerElement != null)
  325. throw new InvalidOperationException (
  326. "Specified attribute is already an attribute of another element.");
  327. XmlAttribute ret = Attributes.SetNamedItem (newAttr) as XmlAttribute;
  328. return ret == newAttr ? null : ret;
  329. }
  330. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  331. {
  332. // Note that this constraint is only for this method.
  333. // SetAttribute() allows prefixed name.
  334. XmlConvert.VerifyNCName (localName);
  335. return Attributes.Append (OwnerDocument.CreateAttribute (String.Empty, localName, namespaceURI, false, true));
  336. }
  337. public override void WriteContentTo (XmlWriter w)
  338. {
  339. int count = ChildNodes.Count;
  340. for (int i = 0; i < count; i++)
  341. ChildNodes [i].WriteTo (w);
  342. }
  343. public override void WriteTo (XmlWriter w)
  344. {
  345. w.WriteStartElement (NamespaceURI == null || NamespaceURI.Length == 0 ? String.Empty : Prefix, LocalName, NamespaceURI);
  346. for (int i = 0; i < Attributes.Count; i++)
  347. if (Attributes [i].Specified)
  348. Attributes [i].WriteTo(w);
  349. if (IsEmpty)
  350. w.WriteEndElement ();
  351. else {
  352. WriteContentTo (w);
  353. w.WriteFullEndElement ();
  354. }
  355. }
  356. #endregion
  357. }
  358. }