XmlElement.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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. using System;
  12. using System.Collections;
  13. using System.Xml.XPath;
  14. using System.IO;
  15. using System.Text;
  16. namespace System.Xml
  17. {
  18. public class XmlElement : XmlLinkedNode
  19. {
  20. #region Fields
  21. private XmlAttributeCollection attributes;
  22. private string localName;
  23. private string namespaceURI;
  24. private string prefix;
  25. private bool isNotEmpty;
  26. #endregion
  27. #region Constructor
  28. protected internal XmlElement (
  29. string prefix,
  30. string localName,
  31. string namespaceURI,
  32. XmlDocument doc) : base (doc)
  33. {
  34. this.prefix = prefix;
  35. this.localName = localName;
  36. this.namespaceURI = namespaceURI;
  37. attributes = new XmlAttributeCollection (this);
  38. // TODO: Adds default attributes
  39. if(doc.DocumentType != null)
  40. {
  41. }
  42. }
  43. #endregion
  44. #region Properties
  45. public override XmlAttributeCollection Attributes {
  46. get { return attributes; }
  47. }
  48. public virtual bool HasAttributes {
  49. get { return attributes.Count > 0; }
  50. }
  51. public override string InnerText {
  52. get {
  53. return base.InnerText;
  54. }
  55. set {
  56. // Why its behavior (of MS FCL) is different from InnerXml...?
  57. if (FirstChild != null && FirstChild.NodeType == XmlNodeType.Text)
  58. FirstChild.Value = value;
  59. else {
  60. if(FirstChild != null) {
  61. foreach (XmlNode n in ChildNodes)
  62. this.RemoveChild (n);
  63. }
  64. // creates new Text node
  65. AppendChild(OwnerDocument.CreateTextNode(value));
  66. }
  67. }
  68. }
  69. public override string InnerXml {
  70. get {
  71. return base.InnerXml;
  72. }
  73. set {
  74. while (FirstChild != null)
  75. this.RemoveChild (FirstChild);
  76. // I hope there are any well-performance logic...
  77. XmlNameTable nt = this.OwnerDocument.NameTable;
  78. XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
  79. XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
  80. OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
  81. BaseURI, XmlLang, XmlSpace, null);
  82. XmlTextReader xmlReader = new XmlTextReader (value, XmlNodeType.Element, ctx);
  83. xmlReader.XmlResolver = OwnerDocument.Resolver;
  84. do {
  85. XmlNode n = OwnerDocument.ReadNode (xmlReader);
  86. if(n == null) break;
  87. AppendChild (n);
  88. } while (true);
  89. }
  90. }
  91. public bool IsEmpty {
  92. get {
  93. return !isNotEmpty && (FirstChild == null);
  94. }
  95. set {
  96. isNotEmpty = !value;
  97. if(value) {
  98. while (FirstChild != null)
  99. RemoveChild (FirstChild);
  100. }
  101. }
  102. }
  103. public override string LocalName {
  104. get { return localName; }
  105. }
  106. public override string Name {
  107. get {
  108. if (prefix == String.Empty || prefix == null)
  109. return localName;
  110. else
  111. return prefix + ":" + localName;
  112. }
  113. }
  114. public override string NamespaceURI {
  115. get { return namespaceURI; }
  116. }
  117. // Why is this override?
  118. public override XmlNode NextSibling {
  119. get {
  120. return base.NextSibling;
  121. }
  122. }
  123. public override XmlNodeType NodeType {
  124. get {
  125. return XmlNodeType.Element;
  126. }
  127. }
  128. internal override XPathNodeType XPathNodeType {
  129. get {
  130. return XPathNodeType.Element;
  131. }
  132. }
  133. public override XmlDocument OwnerDocument {
  134. get {
  135. return base.OwnerDocument;
  136. }
  137. }
  138. public override string Prefix {
  139. get { return prefix; }
  140. set {
  141. if (IsReadOnly)
  142. throw new XmlException ("This node is readonly.");
  143. if (!XmlChar.IsNCName (value))
  144. throw new ArgumentException ("Specified name is not a valid NCName: " + value);
  145. prefix = value;
  146. }
  147. }
  148. #endregion
  149. #region Methods
  150. public override XmlNode CloneNode (bool deep)
  151. {
  152. XmlElement node = new XmlElement (
  153. prefix, localName, namespaceURI, OwnerDocument);
  154. for (int i = 0; i < Attributes.Count; i++)
  155. node.SetAttributeNode ((XmlAttribute)
  156. Attributes [i].CloneNode (true));
  157. if (deep) {
  158. foreach (XmlNode child in this.ChildNodes)
  159. node.AppendChild (child.CloneNode (true));
  160. } // shallow cloning
  161. return node;
  162. }
  163. public virtual string GetAttribute (string name)
  164. {
  165. XmlNode attributeNode = Attributes.GetNamedItem (name);
  166. return attributeNode != null ? attributeNode.Value : String.Empty;
  167. }
  168. public virtual string GetAttribute (string localName, string namespaceURI)
  169. {
  170. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  171. return attributeNode != null ? attributeNode.Value : String.Empty;
  172. }
  173. public virtual XmlAttribute GetAttributeNode (string name)
  174. {
  175. XmlNode attributeNode = Attributes.GetNamedItem (name);
  176. return attributeNode != null ? attributeNode as XmlAttribute : null;
  177. }
  178. public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
  179. {
  180. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  181. return attributeNode != null ? attributeNode as XmlAttribute : null;
  182. }
  183. public virtual XmlNodeList GetElementsByTagName (string name)
  184. {
  185. ArrayList nodeArrayList = new ArrayList ();
  186. this.searchNodesRecursively (this, name, nodeArrayList);
  187. return new XmlNodeArrayList (nodeArrayList);
  188. }
  189. private void searchNodesRecursively (XmlNode argNode, string argName,
  190. ArrayList argArrayList)
  191. {
  192. XmlNodeList xmlNodeList = argNode.ChildNodes;
  193. foreach (XmlNode node in xmlNodeList){
  194. if (node.Name.Equals (argName))
  195. argArrayList.Add (node);
  196. else
  197. this.searchNodesRecursively (node, argName, argArrayList);
  198. }
  199. }
  200. private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
  201. ArrayList argArrayList)
  202. {
  203. XmlNodeList xmlNodeList = argNode.ChildNodes;
  204. foreach (XmlNode node in xmlNodeList)
  205. {
  206. if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
  207. argArrayList.Add (node);
  208. else
  209. this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
  210. }
  211. }
  212. public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
  213. {
  214. ArrayList nodeArrayList = new ArrayList ();
  215. this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
  216. return new XmlNodeArrayList (nodeArrayList);
  217. }
  218. public virtual bool HasAttribute (string name)
  219. {
  220. XmlNode attributeNode = Attributes.GetNamedItem (name);
  221. return attributeNode != null;
  222. }
  223. public virtual bool HasAttribute (string localName, string namespaceURI)
  224. {
  225. XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
  226. return attributeNode != null;
  227. }
  228. public override void RemoveAll ()
  229. {
  230. // Remove the child nodes.
  231. base.RemoveAll ();
  232. // Remove all attributes.
  233. attributes.RemoveAll ();
  234. }
  235. public virtual void RemoveAllAttributes ()
  236. {
  237. attributes.RemoveAll ();
  238. }
  239. public virtual void RemoveAttribute (string name)
  240. {
  241. XmlAttribute attr = attributes.GetNamedItem (name) as XmlAttribute;
  242. if (attr != null)
  243. attributes.Remove(attr);
  244. }
  245. public virtual void RemoveAttribute (string localName, string namespaceURI)
  246. {
  247. XmlAttribute attr = attributes.GetNamedItem(localName, namespaceURI) as XmlAttribute;
  248. if (attr != null)
  249. attributes.Remove(attr);
  250. }
  251. public virtual XmlNode RemoveAttributeAt (int i)
  252. {
  253. return attributes.RemoveAt (i);
  254. }
  255. public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
  256. {
  257. return attributes.Remove(oldAttr);
  258. }
  259. public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
  260. {
  261. return attributes.Remove(attributes[localName, namespaceURI]);
  262. }
  263. public virtual void SetAttribute (string name, string value)
  264. {
  265. XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
  266. attribute.SetOwnerElement(this);
  267. attribute.Value = value;
  268. Attributes.SetNamedItem (attribute);
  269. }
  270. public virtual string SetAttribute (string localName, string namespaceURI, string value)
  271. {
  272. XmlAttribute attr = attributes[localName, namespaceURI];
  273. if(attr == null)
  274. {
  275. attr = OwnerDocument.CreateAttribute(localName, namespaceURI);
  276. attr.Value = value;
  277. attributes.SetNamedItem(attr);
  278. }
  279. else
  280. attr.Value = value;
  281. return attr.Value;
  282. }
  283. public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
  284. {
  285. if (newAttr.OwnerElement != null)
  286. throw new InvalidOperationException (
  287. "Specified attribute is already an attribute of another element.");
  288. newAttr.SetOwnerElement(this);
  289. XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
  290. return oldAttr != null ? oldAttr as XmlAttribute : null;
  291. }
  292. public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
  293. {
  294. XmlDocument xmlDoc = this.OwnerDocument;
  295. XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc);
  296. return this.attributes.Append (xmlAttribute);
  297. }
  298. public override void WriteContentTo (XmlWriter w)
  299. {
  300. foreach(XmlNode childNode in ChildNodes)
  301. childNode.WriteTo(w);
  302. }
  303. public override void WriteTo (XmlWriter w)
  304. {
  305. w.WriteStartElement(Prefix, LocalName, NamespaceURI);
  306. foreach(XmlAttribute attributeNode in Attributes)
  307. if (attributeNode.Specified)
  308. attributeNode.WriteTo(w);
  309. if (IsEmpty)
  310. w.WriteEndElement ();
  311. else {
  312. WriteContentTo(w);
  313. w.WriteFullEndElement();
  314. }
  315. }
  316. #endregion
  317. }
  318. }