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