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 // Why its behavior (of MS FCL) is different from InnerXml...?
70 if (FirstChild != null && FirstChild.NodeType == XmlNodeType.Text)
71 FirstChild.Value = value;
73 if(FirstChild != null) {
74 foreach (XmlNode n in ChildNodes)
77 // creates new Text node
78 AppendChild(OwnerDocument.CreateTextNode(value));
83 public override string InnerXml {
88 foreach(XmlNode n in ChildNodes)
91 // I hope there are any well-performance logic...
92 XmlNameTable nt = this.OwnerDocument.NameTable;
93 XmlNamespaceManager nsmgr = this.ConstructNamespaceManager ();
94 XmlParserContext ctx = new XmlParserContext (nt, nsmgr, XmlLang, this.XmlSpace);
95 XmlTextReader xmlReader = OwnerDocument.ReusableReader;
96 xmlReader.SetReaderContext (String.Empty, ctx);
97 xmlReader.SetReaderFragment (new StringReader (value), XmlNodeType.Element);
100 XmlNode n = OwnerDocument.ReadNode (xmlReader);
107 public bool IsEmpty {
108 get { return isEmpty; }
117 public override string LocalName {
118 get { return localName; }
121 public override string Name {
123 return prefix != String.Empty ? prefix + ":" + localName : localName;
127 public override string NamespaceURI {
128 get { return namespaceURI; }
132 public override XmlNode NextSibling {
134 return base.NextSibling;
138 public override XmlNodeType NodeType {
140 return XmlNodeType.Element;
144 internal override XPathNodeType XPathNodeType {
146 return XPathNodeType.Element;
151 public override XmlDocument OwnerDocument {
153 return base.OwnerDocument;
157 public override string Prefix {
158 get { return prefix; }
159 set { prefix = value; }
167 public override XmlNode CloneNode (bool deep)
169 XmlNode node = new XmlElement (prefix, localName, namespaceURI,
172 for (int i = 0; i < node.Attributes.Count; i++)
173 node.AppendChild (node.Attributes [i].CloneNode (false));
176 while ((node != null) && (node.HasChildNodes)) {
177 AppendChild (node.NextSibling.CloneNode (true));
178 node = node.NextSibling;
183 // Reminder: Also look into Default attributes.
189 public virtual string GetAttribute (string name)
191 XmlNode attributeNode = Attributes.GetNamedItem (name);
192 return attributeNode != null ? attributeNode.Value : String.Empty;
196 public virtual string GetAttribute (string localName, string namespaceURI)
198 XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
199 return attributeNode != null ? attributeNode.Value : String.Empty;
203 public virtual XmlAttribute GetAttributeNode (string name)
205 XmlNode attributeNode = Attributes.GetNamedItem (name);
206 return attributeNode != null ? attributeNode as XmlAttribute : null;
210 public virtual XmlAttribute GetAttributeNode (string localName, string namespaceURI)
212 XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
213 return attributeNode != null ? attributeNode as XmlAttribute : null;
216 public virtual XmlNodeList GetElementsByTagName (string name)
218 ArrayList nodeArrayList = new ArrayList ();
219 this.searchNodesRecursively (this, name, nodeArrayList);
220 return new XmlNodeArrayList (nodeArrayList);
223 private void searchNodesRecursively (XmlNode argNode, string argName,
224 ArrayList argArrayList)
226 XmlNodeList xmlNodeList = argNode.ChildNodes;
227 foreach (XmlNode node in xmlNodeList){
228 if (node.Name.Equals (argName))
229 argArrayList.Add (node);
231 this.searchNodesRecursively (node, argName, argArrayList);
235 private void searchNodesRecursively (XmlNode argNode, string argName, string argNamespaceURI,
236 ArrayList argArrayList)
238 XmlNodeList xmlNodeList = argNode.ChildNodes;
239 foreach (XmlNode node in xmlNodeList)
241 if (node.LocalName.Equals (argName) && node.NamespaceURI.Equals (argNamespaceURI))
242 argArrayList.Add (node);
244 this.searchNodesRecursively (node, argName, argNamespaceURI, argArrayList);
248 public virtual XmlNodeList GetElementsByTagName (string localName, string namespaceURI)
250 ArrayList nodeArrayList = new ArrayList ();
251 this.searchNodesRecursively (this, localName, namespaceURI, nodeArrayList);
252 return new XmlNodeArrayList (nodeArrayList);
256 public virtual bool HasAttribute (string name)
258 XmlNode attributeNode = Attributes.GetNamedItem (name);
259 return attributeNode != null;
263 public virtual bool HasAttribute (string localName, string namespaceURI)
265 XmlNode attributeNode = Attributes.GetNamedItem (localName, namespaceURI);
266 return attributeNode != null;
269 [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
270 public override void RemoveAll ()
272 // Remove the child nodes.
275 // Remove all attributes.
276 attributes.RemoveAll ();
279 [MonoTODO ("confirm not removing default attributes [when DTD feature was implemented.")]
280 public virtual void RemoveAllAttributes ()
282 attributes.RemoveAll ();
285 [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
286 public virtual void RemoveAttribute (string name)
288 attributes.Remove((XmlAttribute)attributes.GetNamedItem(name));
291 [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
292 public virtual void RemoveAttribute (string localName, string namespaceURI)
294 attributes.Remove((XmlAttribute)attributes.GetNamedItem(localName, namespaceURI));
297 [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
298 public virtual XmlNode RemoveAttributeAt (int i)
300 return attributes.Remove(attributes[i]);
303 [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
304 public virtual XmlAttribute RemoveAttributeNode (XmlAttribute oldAttr)
306 return attributes.Remove(oldAttr);
309 [MonoTODO ("confirm not resetting default attributes [when DTD feature was implemented.")]
310 public virtual XmlAttribute RemoveAttributeNode (string localName, string namespaceURI)
312 return attributes.Remove(attributes[localName, namespaceURI]);
316 public virtual void SetAttribute (string name, string value)
318 XmlAttribute attribute = OwnerDocument.CreateAttribute (name);
319 attribute.SetOwnerElement(this);
320 attribute.Value = value;
321 Attributes.SetNamedItem (attribute);
325 public virtual string SetAttribute (string localName, string namespaceURI, string value)
327 XmlAttribute attr = attributes[localName, namespaceURI];
330 attr = OwnerDocument.CreateAttribute(localName, namespaceURI);
332 attributes.SetNamedItem(attr);
340 public virtual XmlAttribute SetAttributeNode (XmlAttribute newAttr)
342 newAttr.SetOwnerElement(this);
343 XmlNode oldAttr = Attributes.SetNamedItem(newAttr);
344 return oldAttr != null ? oldAttr as XmlAttribute : null;
347 public virtual XmlAttribute SetAttributeNode (string localName, string namespaceURI)
349 XmlDocument xmlDoc = this.OwnerDocument;
350 XmlAttribute xmlAttribute = new XmlAttribute (String.Empty, localName, namespaceURI, xmlDoc);
351 return this.attributes.Append (xmlAttribute);
354 public override void WriteContentTo (XmlWriter w)
356 foreach(XmlNode childNode in ChildNodes)
357 childNode.WriteTo(w);
361 public override void WriteTo (XmlWriter w)
363 w.WriteStartElement(Prefix, LocalName, NamespaceURI);
365 // write namespace declarations(if not exist)
366 if(Prefix != null && w.LookupPrefix (Prefix) != NamespaceURI)
367 w.WriteAttributeString ("xmlns", Prefix, "http://www.w3.org/2000/xmlns/", NamespaceURI);
369 foreach(XmlNode attributeNode in Attributes)
371 attributeNode.WriteTo(w);
372 // write namespace declarations(if not exist)
373 if(attributeNode.Prefix != null &&
\r
374 w.LookupPrefix(attributeNode.Prefix) != attributeNode.NamespaceURI &&
\r
375 attributeNode.Prefix != "xmlns")
\r
376 w.WriteAttributeString("xmlns", attributeNode.Prefix, "http://www.w3.org/2000/xmlns/", attributeNode.NamespaceURI);