2 // System.Xml.XmlAttribute
5 // Jason Diamond (jason@injektilo.org)
7 // (C) 2002 Jason Diamond http://injektilo.org/
11 using System.Collections;
15 public class XmlElement : XmlLinkedNode
19 private XmlAttributeCollection attributes;
20 private XmlLinkedNode lastLinkedChild;
21 private string localName;
22 private string namespaceURI;
23 private string prefix;
29 protected internal XmlElement (
33 XmlDocument doc) : base (doc)
36 this.localName = localName;
37 this.namespaceURI = namespaceURI;
39 attributes = new XmlAttributeCollection (this);
46 public override XmlAttributeCollection Attributes {
47 get { return attributes; }
50 public virtual bool HasAttributes {
51 get { return attributes.Count > 0; }
54 [MonoTODO ("Setter.")]
55 public override string InnerXml {
57 // Not sure why this is an override. Passing through for now.
60 set { throw new NotImplementedException (); }
65 get { throw new NotImplementedException (); }
67 set { throw new NotImplementedException (); }
70 internal override XmlLinkedNode LastLinkedChild {
71 get { return lastLinkedChild; }
73 set { lastLinkedChild = value; }
76 public override string LocalName {
77 get { return localName; }
80 public override string Name {
82 return prefix != String.Empty ? prefix + ":" + localName : localName;
86 public override string NamespaceURI {
87 get { return namespaceURI; }
91 public override XmlNode NextSibling {
93 return base.NextSibling;
97 public override XmlNodeType NodeType {
99 return XmlNodeType.Element;
104 public override XmlDocument OwnerDocument {
106 return base.OwnerDocument;
110 public override string Prefix {
121 public override XmlNode CloneNode (bool deep)
123 XmlNode node = new XmlElement (prefix, localName, namespaceURI,
126 for (int i = 0; i < node.Attributes.Count; i++)
127 node.AppendChild (node.Attributes [i].CloneNode (false));
130 while ((node != null) && (node.HasChildNodes)) {
131 AppendChild (node.NextSibling.CloneNode (true));
132 node = node.NextSibling;
137 // Reminder: Also look into Default attributes.
143 public virtual string GetAttribute (string name)
145 XmlNode attributeNode = Attributes.GetNamedItem (name);
146 return attributeNode != null ? attributeNode.Value : String.Empty;
150 public virtual string GetAttribute (string localName, string namespaceURI)
152 XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
153 return attributeNode != null ? attributeNode.Value : String.Empty;
157 public virtual XmlAttribute GetAttributeNode (string name)
159 XmlNode attributeNode = Attributes.GetNamedItem (name);
160 return attributeNode != null ? attributeNode as XmlAttribute : null;
164 public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
166 XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
167 return attributeNode != null ? attributeNode as XmlAttribute : null;
170 public virtual XmlNodeList GetElementsByTagName (string name)
172 ArrayList nodeArrayList = new ArrayList ();
173 this.searchNodesRecursively (this, name, String.Empty, nodeArrayList);
174 return new XmlNodeArrayList (nodeArrayList);
177 private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
178 ArrayList argArrayList)
180 XmlNodeList xmlNodeList = argNode.ChildNodes;
181 foreach (XmlNode node in xmlNodeList)
183 if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
184 argArrayList.Add (node);
186 this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
191 public virtual bool HasAttribute (string name)
193 XmlNode attributeNode = Attributes.GetNamedItem (name);
194 return attributeNode != null;
198 public virtual bool HasAttribute (string localName, string namespaceURI)
200 throw new NotImplementedException ();
203 [MonoTODO ("Don't remove default attributes.")]
204 public override void RemoveAll ()
206 // Remove the child nodes.
209 // Remove all attributes.
210 attributes.RemoveAll ();
214 public virtual void RemoveAllAttributes ()
216 throw new NotImplementedException ();
220 public virtual void RemoveAttribute (string name)
222 throw new NotImplementedException ();
226 public virtual void RemoveAttribute (string localName, string namespaceURI)
228 throw new NotImplementedException ();
232 public virtual XmlNode RemoveAttributeAt (int i)
234 throw new NotImplementedException ();
238 public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
240 throw new NotImplementedException ();
244 public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
246 throw new NotImplementedException ();
250 public virtual void SetAttribute (string name, string value)
252 XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
253 attribute.SetParentNode (this);
254 attribute.Value = value;
255 Attributes.SetNamedItem (attribute);
259 public virtual string SetAttribute (string localName, string namespaceURI, string value)
261 throw new NotImplementedException ();
265 public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
267 newAttr.SetParentNode(this);
268 XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
269 return oldAttr != null ? oldAttr as XmlAttribute : null;
273 public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
275 throw new NotImplementedException ();
278 public override void WriteContentTo (XmlWriter w)
280 foreach(XmlNode childNode in ChildNodes)
281 childNode.WriteTo(w);
284 public override void WriteTo (XmlWriter w)
286 w.WriteStartElement(Prefix, LocalName, NamespaceURI);
288 foreach(XmlNode attributeNode in Attributes)
289 attributeNode.WriteTo(w);