XmlElement.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 (FirstChild != null && FirstChild.NextSibling == null && 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. public override XmlNode ParentNode {
  198. get { return base.ParentNode; }
  199. }
  200. public override IXmlSchemaInfo SchemaInfo {
  201. get { return schemaInfo; }
  202. internal set { schemaInfo = value; }
  203. }
  204. #endif
  205. #endregion
  206. #region Methods
  207. public override XmlNode CloneNode (bool deep)
  208. {
  209. XmlElement node = new XmlElement (
  210. name.Prefix, name.LocalName, name.NS, OwnerDocument, true);
  211. for (int i = 0; i < Attributes.Count; i++)
  212. node.SetAttributeNode ((XmlAttribute)
  213. Attributes [i].CloneNode (true));
  214. if (deep) {
  215. for (int i = 0; i < ChildNodes.Count; i++)
  216. node.AppendChild (ChildNodes [i].CloneNode (true));
  217. }
  218. return node;
  219. }
  220. public virtual string GetAttribute (string name)
  221. {
  222. XmlNode attributeNode = Attributes.GetNamedItem (name);
  223. return attributeNode != null ? attributeNode.Value : String.Empty;
  224. }
  225. public virtual string GetAttribute (string localName, string namespaceURI)
  226. {
  227. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  228. return attributeNode != null ? attributeNode.Value : String.Empty;
  229. }
  230. public virtual XmlAttribute GetAttributeNode (string name)
  231. {
  232. XmlNode attributeNode = Attributes.GetNamedItem (name);
  233. return attributeNode != null ? attributeNode as XmlAttribute : null;
  234. }
  235. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  236. {
  237. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  238. return attributeNode != null ? attributeNode as XmlAttribute : null;
  239. }
  240. public virtual XmlNodeList GetElementsByTagName (string name)
  241. {
  242. ArrayList nodeArrayList = new ArrayList ();
  243. this.SearchDescendantElements (name, name == "*", nodeArrayList);
  244. return new XmlNodeArrayList (nodeArrayList);
  245. }
  246. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  247. {
  248. ArrayList nodeArrayList = new ArrayList ();
  249. this.SearchDescendantElements (localName, localName == "*", namespaceURI, namespaceURI == "*", nodeArrayList);
  250. return new XmlNodeArrayList (nodeArrayList);
  251. }
  252. public virtual bool HasAttribute (string name)
  253. {
  254. XmlNode attributeNode = Attributes.GetNamedItem (name);
  255. return attributeNode != null;
  256. }
  257. public virtual bool HasAttribute (string localName, string namespaceURI)
  258. {
  259. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  260. return attributeNode != null;
  261. }
  262. public override void RemoveAll ()
  263. {
  264. // Remove all attributes and child nodes.
  265. base.RemoveAll ();
  266. }
  267. public virtual void RemoveAllAttributes ()
  268. {
  269. if (attributes != null)
  270. attributes.RemoveAll ();
  271. }
  272. public virtual void RemoveAttribute (string name)
  273. {
  274. if (attributes == null)
  275. return;
  276. XmlAttribute attr = Attributes.GetNamedItem (name) as XmlAttribute;
  277. if (attr != null)
  278. Attributes.Remove(attr);
  279. }
  280. public virtual void RemoveAttribute (string localName, string namespaceURI)
  281. {
  282. if (attributes == null)
  283. return;
  284. XmlAttribute attr = attributes.GetNamedItem(localName, namespaceURI) as XmlAttribute;
  285. if (attr != null)
  286. Attributes.Remove(attr);
  287. }
  288. public virtual XmlNode RemoveAttributeAt (int i)
  289. {
  290. if (attributes == null || attributes.Count <= i)
  291. return null;
  292. return Attributes.RemoveAt (i);
  293. }
  294. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  295. {
  296. if (attributes == null)
  297. return null;
  298. return Attributes.Remove (oldAttr);
  299. }
  300. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  301. {
  302. if (attributes == null)
  303. return null;
  304. return Attributes.Remove (attributes [localName, namespaceURI]);
  305. }
  306. public virtual void SetAttribute (string name, string value)
  307. {
  308. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  309. attribute.Value = value;
  310. Attributes.SetNamedItem (attribute);
  311. }
  312. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  313. {
  314. XmlAttribute attr = Attributes [localName, namespaceURI];
  315. if (attr == null) {
  316. attr = OwnerDocument.CreateAttribute (localName, namespaceURI);
  317. attr.Value = value;
  318. Attributes.SetNamedItem (attr);
  319. }
  320. else
  321. attr.Value = value;
  322. return attr.Value;
  323. }
  324. public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
  325. {
  326. if (newAttr.OwnerElement != null)
  327. throw new InvalidOperationException (
  328. "Specified attribute is already an attribute of another element.");
  329. XmlAttribute ret = Attributes.SetNamedItem (newAttr) as XmlAttribute;
  330. return ret == newAttr ? null : ret;
  331. }
  332. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  333. {
  334. // Note that this constraint is only for this method.
  335. // SetAttribute() allows prefixed name.
  336. XmlConvert.VerifyNCName (localName);
  337. return Attributes.Append (OwnerDocument.CreateAttribute (String.Empty, localName, namespaceURI, false, true));
  338. }
  339. public override void WriteContentTo (XmlWriter w)
  340. {
  341. int count = ChildNodes.Count;
  342. for (int i = 0; i < count; i++)
  343. ChildNodes [i].WriteTo (w);
  344. }
  345. public override void WriteTo (XmlWriter w)
  346. {
  347. w.WriteStartElement (NamespaceURI == null || NamespaceURI.Length == 0 ? String.Empty : Prefix, LocalName, NamespaceURI);
  348. for (int i = 0; i < Attributes.Count; i++)
  349. if (Attributes [i].Specified)
  350. Attributes [i].WriteTo(w);
  351. if (IsEmpty)
  352. w.WriteEndElement ();
  353. else {
  354. WriteContentTo (w);
  355. w.WriteFullEndElement ();
  356. }
  357. }
  358. #endregion
  359. }
  360. }