XmlElement.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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, IHasXmlChildNode
  43. {
  44. #region Fields
  45. private XmlAttributeCollection attributes;
  46. private XmlNameEntry name;
  47. XmlLinkedNode lastLinkedChild;
  48. private bool isNotEmpty;
  49. #if NET_2_0
  50. IXmlSchemaInfo schemaInfo;
  51. #endif
  52. #endregion
  53. #region Constructor
  54. protected internal XmlElement (
  55. string prefix,
  56. string localName,
  57. string namespaceURI,
  58. XmlDocument doc) : this (prefix, localName, namespaceURI, doc, false)
  59. {
  60. }
  61. internal XmlElement (
  62. string prefix,
  63. string localName,
  64. string namespaceURI,
  65. XmlDocument doc,
  66. bool atomizedNames) : base (doc)
  67. {
  68. if (!atomizedNames) {
  69. XmlConvert.VerifyName (localName);
  70. prefix = doc.NameTable.Add (prefix);
  71. localName = doc.NameTable.Add (localName);
  72. namespaceURI = doc.NameTable.Add (namespaceURI);
  73. }
  74. name = doc.NameCache.Add (prefix, localName, namespaceURI, true);
  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. XmlLinkedNode IHasXmlChildNode.LastLinkedChild {
  92. get { return lastLinkedChild; }
  93. set { lastLinkedChild = value; }
  94. }
  95. public override XmlAttributeCollection Attributes {
  96. get {
  97. if (attributes == null)
  98. attributes = new XmlAttributeCollection (this);
  99. return attributes;
  100. }
  101. }
  102. public virtual bool HasAttributes {
  103. get { return attributes != null && attributes.Count > 0; }
  104. }
  105. public override string InnerText {
  106. get {
  107. return base.InnerText;
  108. }
  109. set {
  110. // Why its behavior (of MS FCL) is different from InnerXml...?
  111. if (FirstChild != null && FirstChild.NextSibling == null && FirstChild.NodeType == XmlNodeType.Text)
  112. FirstChild.Value = value;
  113. else {
  114. while (FirstChild != null)
  115. this.RemoveChild (FirstChild);
  116. // creates new Text node
  117. AppendChild (OwnerDocument.CreateTextNode (value), false);
  118. }
  119. }
  120. }
  121. public override string InnerXml {
  122. get {
  123. return base.InnerXml;
  124. }
  125. set {
  126. while (FirstChild != null)
  127. this.RemoveChild (FirstChild);
  128. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  129. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  130. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  131. BaseURI, XmlLang, XmlSpace, null);
  132. XmlTextReader xmlReader = new XmlTextReader (value, XmlNodeType.Element, ctx);
  133. xmlReader.XmlResolver = OwnerDocument.Resolver;
  134. do {
  135. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  136. if(n == null) break;
  137. AppendChild (n);
  138. } while (true);
  139. }
  140. }
  141. public bool IsEmpty {
  142. get {
  143. return !isNotEmpty && (FirstChild == null);
  144. }
  145. set {
  146. isNotEmpty = !value;
  147. if(value) {
  148. while (FirstChild != null)
  149. RemoveChild (FirstChild);
  150. }
  151. }
  152. }
  153. public override string LocalName {
  154. get { return name.LocalName; }
  155. }
  156. public override string Name {
  157. get {
  158. if (name.Prefix == String.Empty || name.Prefix == null)
  159. return name.LocalName;
  160. else
  161. return OwnerDocument.NameTable.Add (name.Prefix + ":" + name.LocalName);
  162. }
  163. }
  164. public override string NamespaceURI {
  165. get { return name.NS; }
  166. }
  167. // Why is this override?
  168. public override XmlNode NextSibling {
  169. get {
  170. return base.NextSibling;
  171. }
  172. }
  173. public override XmlNodeType NodeType {
  174. get {
  175. return XmlNodeType.Element;
  176. }
  177. }
  178. internal override XPathNodeType XPathNodeType {
  179. get {
  180. return XPathNodeType.Element;
  181. }
  182. }
  183. public override XmlDocument OwnerDocument {
  184. get {
  185. return base.OwnerDocument;
  186. }
  187. }
  188. public override string Prefix {
  189. get { return name.Prefix; }
  190. set {
  191. if (IsReadOnly)
  192. throw new ArgumentException ("This node is readonly.");
  193. if (value == null) {
  194. #if NET_2_0
  195. value = string.Empty;
  196. #else
  197. throw new ArgumentNullException ("Prefix value is null.");
  198. #endif
  199. }
  200. if ((!String.Empty.Equals(value))&&(!XmlChar.IsNCName (value)))
  201. throw new ArgumentException ("Specified name is not a valid NCName: " + value);
  202. value = OwnerDocument.NameTable.Add (value);
  203. name = OwnerDocument.NameCache.Add (value,
  204. name.LocalName, name.NS, true);
  205. }
  206. }
  207. #if NET_2_0
  208. public override XmlNode ParentNode {
  209. get { return base.ParentNode; }
  210. }
  211. public override IXmlSchemaInfo SchemaInfo {
  212. get { return schemaInfo; }
  213. internal set { schemaInfo = value; }
  214. }
  215. #endif
  216. #endregion
  217. #region Methods
  218. public override XmlNode CloneNode (bool deep)
  219. {
  220. XmlElement node = new XmlElement (
  221. name.Prefix, name.LocalName, name.NS, OwnerDocument, true);
  222. for (int i = 0; i < Attributes.Count; i++)
  223. node.SetAttributeNode ((XmlAttribute)
  224. Attributes [i].CloneNode (true));
  225. if (deep) {
  226. for (int i = 0; i < ChildNodes.Count; i++)
  227. node.AppendChild (ChildNodes [i].CloneNode (true), false);
  228. }
  229. return node;
  230. }
  231. public virtual string GetAttribute (string name)
  232. {
  233. XmlNode attributeNode = Attributes.GetNamedItem (name);
  234. return attributeNode != null ? attributeNode.Value : String.Empty;
  235. }
  236. public virtual string GetAttribute (string localName, string namespaceURI)
  237. {
  238. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  239. return attributeNode != null ? attributeNode.Value : String.Empty;
  240. }
  241. public virtual XmlAttribute GetAttributeNode (string name)
  242. {
  243. XmlNode attributeNode = Attributes.GetNamedItem (name);
  244. return attributeNode != null ? attributeNode as XmlAttribute : null;
  245. }
  246. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  247. {
  248. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  249. return attributeNode != null ? attributeNode as XmlAttribute : null;
  250. }
  251. public virtual XmlNodeList GetElementsByTagName (string name)
  252. {
  253. ArrayList nodeArrayList = new ArrayList ();
  254. this.SearchDescendantElements (name, name == "*", nodeArrayList);
  255. return new XmlNodeArrayList (nodeArrayList);
  256. }
  257. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  258. {
  259. ArrayList nodeArrayList = new ArrayList ();
  260. this.SearchDescendantElements (localName, localName == "*", namespaceURI, namespaceURI == "*", nodeArrayList);
  261. return new XmlNodeArrayList (nodeArrayList);
  262. }
  263. public virtual bool HasAttribute (string name)
  264. {
  265. XmlNode attributeNode = Attributes.GetNamedItem (name);
  266. return attributeNode != null;
  267. }
  268. public virtual bool HasAttribute (string localName, string namespaceURI)
  269. {
  270. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  271. return attributeNode != null;
  272. }
  273. public override void RemoveAll ()
  274. {
  275. // Remove all attributes and child nodes.
  276. base.RemoveAll ();
  277. }
  278. public virtual void RemoveAllAttributes ()
  279. {
  280. if (attributes != null)
  281. attributes.RemoveAll ();
  282. }
  283. public virtual void RemoveAttribute (string name)
  284. {
  285. if (attributes == null)
  286. return;
  287. XmlAttribute attr = Attributes.GetNamedItem (name) as XmlAttribute;
  288. if (attr != null)
  289. Attributes.Remove(attr);
  290. }
  291. public virtual void RemoveAttribute (string localName, string namespaceURI)
  292. {
  293. if (attributes == null)
  294. return;
  295. XmlAttribute attr = attributes.GetNamedItem(localName, namespaceURI) as XmlAttribute;
  296. if (attr != null)
  297. Attributes.Remove(attr);
  298. }
  299. public virtual XmlNode RemoveAttributeAt (int i)
  300. {
  301. if (attributes == null || attributes.Count <= i)
  302. return null;
  303. return Attributes.RemoveAt (i);
  304. }
  305. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  306. {
  307. if (attributes == null)
  308. return null;
  309. return Attributes.Remove (oldAttr);
  310. }
  311. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  312. {
  313. if (attributes == null)
  314. return null;
  315. return Attributes.Remove (attributes [localName, namespaceURI]);
  316. }
  317. public virtual void SetAttribute (string name, string value)
  318. {
  319. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  320. attribute.Value = value;
  321. Attributes.SetNamedItem (attribute);
  322. }
  323. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  324. {
  325. XmlAttribute attr = Attributes [localName, namespaceURI];
  326. if (attr == null) {
  327. attr = OwnerDocument.CreateAttribute (localName, namespaceURI);
  328. attr.Value = value;
  329. Attributes.SetNamedItem (attr);
  330. }
  331. else
  332. attr.Value = value;
  333. return attr.Value;
  334. }
  335. public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
  336. {
  337. if (newAttr.OwnerElement != null)
  338. throw new InvalidOperationException (
  339. "Specified attribute is already an attribute of another element.");
  340. XmlAttribute ret = Attributes.SetNamedItem (newAttr) as XmlAttribute;
  341. return ret == newAttr ? null : ret;
  342. }
  343. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  344. {
  345. // Note that this constraint is only for this method.
  346. // SetAttribute() allows prefixed name.
  347. XmlConvert.VerifyNCName (localName);
  348. return Attributes.Append (OwnerDocument.CreateAttribute (String.Empty, localName, namespaceURI, false, true));
  349. }
  350. public override void WriteContentTo (XmlWriter w)
  351. {
  352. for (XmlNode n = FirstChild; n != null; n = n.NextSibling)
  353. n.WriteTo (w);
  354. }
  355. public override void WriteTo (XmlWriter w)
  356. {
  357. w.WriteStartElement (NamespaceURI == null || NamespaceURI.Length == 0 ? String.Empty : Prefix, LocalName, NamespaceURI);
  358. for (int i = 0; i < Attributes.Count; i++)
  359. if (Attributes [i].Specified)
  360. Attributes [i].WriteTo(w);
  361. if (IsEmpty)
  362. w.WriteEndElement ();
  363. else {
  364. WriteContentTo (w);
  365. w.WriteFullEndElement ();
  366. }
  367. }
  368. #endregion
  369. }
  370. }