XmlElement.cs 11 KB

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