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, nodeArrayList);
174 return new XmlNodeArrayList (nodeArrayList);
177 private void searchNodesRecursively (XmlNode argNode, string argName, ArrayList argArrayList)
179 XmlNodeList xmlNodeList = argNode.ChildNodes;
180 foreach (XmlNode node in xmlNodeList){
181 if (node.Name.Equals (argName))
182 argArrayList.Add (node);
184 this.searchNodesRecursively (node, argName, argArrayList);
189 public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
191 throw new NotImplementedException ();
195 public virtual bool HasAttribute (string name)
197 XmlNode attributeNode = Attributes.GetNamedItem (name);
198 return attributeNode != null;
202 public virtual bool HasAttribute (string localName, string namespaceURI)
204 throw new NotImplementedException ();
207 [MonoTODO ("Don't remove default attributes.")]
208 public override void RemoveAll ()
210 // Remove the child nodes.
213 // Remove all attributes.
214 attributes.RemoveAll ();
218 public virtual void RemoveAllAttributes ()
220 throw new NotImplementedException ();
224 public virtual void RemoveAttribute (string name)
226 throw new NotImplementedException ();
230 public virtual void RemoveAttribute (string localName, string namespaceURI)
232 throw new NotImplementedException ();
236 public virtual XmlNode RemoveAttributeAt (int i)
238 throw new NotImplementedException ();
242 public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
244 throw new NotImplementedException ();
248 public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
250 throw new NotImplementedException ();
254 public virtual void SetAttribute (string name, string value)
256 XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
257 attribute.SetParentNode (this);
258 attribute.Value = value;
259 Attributes.SetNamedItem (attribute);
263 public virtual string SetAttribute (string localName, string namespaceURI, string value)
265 throw new NotImplementedException ();
269 public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
271 newAttr.SetParentNode(this);
272 XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
273 return oldAttr != null ? oldAttr as XmlAttribute : null;
277 public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
279 throw new NotImplementedException ();
282 public override void WriteContentTo (XmlWriter w)
284 foreach(XmlNode childNode in ChildNodes)
285 childNode.WriteTo(w);
288 public override void WriteTo (XmlWriter w)
290 w.WriteStartElement(Prefix, LocalName, NamespaceURI);
292 foreach(XmlNode attributeNode in Attributes)
293 attributeNode.WriteTo(w);