2 // System.Xml.XmlElement
5 // Jason Diamond (jason@injektilo.org)
6 // Atsushi Enomoto (ginga@kit.hi-ho.ne.jp)
8 // (C) 2002 Jason Diamond http://injektilo.org/
9 // (C) 2002 Atsushi Enomoto
13 using System.Collections;
14 using System.Xml.XPath;
20 public class XmlElement : XmlLinkedNode
24 private XmlAttributeCollection attributes;
25 private string localName;
26 private string namespaceURI;
27 private string prefix;
34 protected internal XmlElement (
38 XmlDocument doc) : base (doc)
41 this.localName = localName;
42 this.namespaceURI = namespaceURI;
44 attributes = new XmlAttributeCollection (this);
46 // TODO: Adds default attributes
47 if(doc.DocumentType != null)
56 public override XmlAttributeCollection Attributes {
57 get { return attributes; }
60 public virtual bool HasAttributes {
61 get { return attributes.Count > 0; }
64 public override string InnerText {
66 return base.InnerText;
69 foreach(XmlNode n in ChildNodes)
73 // creates new Text node
74 AppendChild(OwnerDocument.CreateTextNode(value));
78 [MonoTODO ("Setter is immature")]
79 public override string InnerXml {
81 // Not sure why this is an override. Passing through for now.
85 foreach(XmlNode n in ChildNodes)
90 // I hope there are any well-performance logic...
91 XmlNameTable nt = this.OwnerDocument.NameTable;
92 XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
93 XmlParserContext ctx = new XmlParserContext (nt, nsmgr, XmlLang, this.XmlSpace);
94 XmlTextReader xmlReader = OwnerDocument.ReusableReader;
95 xmlReader.SetReaderContext (String.Empty, ctx);
96 xmlReader.SetReaderFragment (new StringReader (value), XmlNodeType.Element);
97 this.ConstructDOM (xmlReader, this);
101 public bool IsEmpty {
102 get { return isEmpty; }
112 public override string LocalName {
113 get { return localName; }
116 public override string Name {
118 return prefix != String.Empty ? prefix + ":" + localName : localName;
122 public override string NamespaceURI {
123 get { return namespaceURI; }
127 public override XmlNode NextSibling {
129 return base.NextSibling;
133 public override XmlNodeType NodeType {
135 return XmlNodeType.Element;
139 internal override XPathNodeType XPathNodeType {
141 return XPathNodeType.Element;
146 public override XmlDocument OwnerDocument {
148 return base.OwnerDocument;
152 public override string Prefix {
153 get { return prefix; }
154 set { prefix = value; }
162 public override XmlNode CloneNode (bool deep)
164 XmlNode node = new XmlElement (prefix, localName, namespaceURI,
167 for (int i = 0; i < node.Attributes.Count; i++)
168 node.AppendChild (node.Attributes [i].CloneNode (false));
171 while ((node != null) && (node.HasChildNodes)) {
172 AppendChild (node.NextSibling.CloneNode (true));
173 node = node.NextSibling;
178 // Reminder: Also look into Default attributes.
184 public virtual string GetAttribute (string name)
186 XmlNode attributeNode = Attributes.GetNamedItem (name);
187 return attributeNode != null ? attributeNode.Value : String.Empty;
191 public virtual string GetAttribute (string localName, string namespaceURI)
193 XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
194 return attributeNode != null ? attributeNode.Value : String.Empty;
198 public virtual XmlAttribute GetAttributeNode (string name)
200 XmlNode attributeNode = Attributes.GetNamedItem (name);
201 return attributeNode != null ? attributeNode as XmlAttribute : null;
205 public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
207 XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
208 return attributeNode != null ? attributeNode as XmlAttribute : null;
211 public virtual XmlNodeList GetElementsByTagName (string name)
213 ArrayList nodeArrayList = new ArrayList ();
214 this.searchNodesRecursively (this, name, nodeArrayList);
215 return new XmlNodeArrayList (nodeArrayList);
218 private void searchNodesRecursively (XmlNode argNode, string argName,
219 ArrayList argArrayList)
221 XmlNodeList xmlNodeList = argNode.ChildNodes;
222 foreach (XmlNode node in xmlNodeList){
223 if (node.Name.Equals (argName))
224 argArrayList.Add (node);
226 this.searchNodesRecursively (node, argName, argArrayList);
230 private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
231 ArrayList argArrayList)
233 XmlNodeList xmlNodeList = argNode.ChildNodes;
234 foreach (XmlNode node in xmlNodeList)
236 if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
237 argArrayList.Add (node);
239 this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
243 public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
245 ArrayList nodeArrayList = new ArrayList ();
246 this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
247 return new XmlNodeArrayList (nodeArrayList);
251 public virtual bool HasAttribute (string name)
253 XmlNode attributeNode = Attributes.GetNamedItem (name);
254 return attributeNode != null;
258 public virtual bool HasAttribute (string localName, string namespaceURI)
260 XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
261 return attributeNode != null;
264 [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
265 public override void RemoveAll ()
267 // Remove the child nodes.
270 // Remove all attributes.
271 attributes.RemoveAll ();
274 [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
275 public virtual void RemoveAllAttributes ()
277 attributes.RemoveAll ();
280 [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
281 public virtual void RemoveAttribute (string name)
283 attributes.Remove((XmlAttribute)attributes.GetNamedItem(name));
286 [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
287 public virtual void RemoveAttribute (string localName, string namespaceURI)
289 attributes.Remove((XmlAttribute)attributes.GetNamedItem(localName, namespaceURI));
292 [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
293 public virtual XmlNode RemoveAttributeAt (int i)
295 return attributes.Remove(attributes[i]);
298 [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
299 public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
301 return attributes.Remove(oldAttr);
304 [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
305 public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
307 return attributes.Remove(attributes[localName, namespaceURI]);
311 public virtual void SetAttribute (string name, string value)
313 XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
314 attribute.SetOwnerElement(this);
315 attribute.Value = value;
316 Attributes.SetNamedItem (attribute);
320 public virtual string SetAttribute (string localName, string namespaceURI, string value)
322 XmlAttribute attr = attributes[localName, namespaceURI];
325 attr = OwnerDocument.CreateAttribute(localName, namespaceURI);
327 attributes.SetNamedItem(attr);
335 public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
337 newAttr.SetOwnerElement(this);
338 XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
339 return oldAttr != null ? oldAttr as XmlAttribute : null;
342 public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
344 XmlDocument xmlDoc = this.OwnerDocument;
345 XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc);
346 return this.attributes.Append (xmlAttribute);
349 public override void WriteContentTo (XmlWriter w)
351 foreach(XmlNode childNode in ChildNodes)
352 childNode.WriteTo(w);
355 [MonoTODO("indenting feature is incomplete.")]
356 public override void WriteTo (XmlWriter w)
358 w.WriteStartElement(Prefix, LocalName, NamespaceURI);
360 // write namespace declarations(if not exist)
361 if(Prefix != null && Prefix != String.Empty && w.LookupPrefix(Prefix) != NamespaceURI)
362 w.WriteAttributeString("xmlns", Prefix, "http://www.w3.org/2000/xmlns/", NamespaceURI);
364 foreach(XmlNode attributeNode in Attributes)
366 attributeNode.WriteTo(w);
367 // write namespace declarations(if not exist)
368 if(attributeNode.Prefix != null && attributeNode.Prefix != String.Empty &&
369 w.LookupPrefix(attributeNode.Prefix) != attributeNode.NamespaceURI &&
370 attributeNode.Prefix != "xmlns")
371 w.WriteAttributeString("xmlns", attributeNode.Prefix, "http://www.w3.org/2000/xmlns/", attributeNode.NamespaceURI);