2 // System.Xml.XmlAttribute
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) 2003 Atsushi Enomoto
14 using System.Xml.XPath;
18 public class XmlAttribute : XmlNode
22 private XmlLinkedNode lastChild;
23 private string localName;
24 private string namespaceURI;
25 private string prefix;
26 internal bool isDefault;
27 private XmlElement ownerElement;
33 protected internal XmlAttribute (
37 XmlDocument doc) : base (doc)
40 prefix = String.Empty;
41 if (namespaceURI == null)
42 namespaceURI = String.Empty;
44 // Prefix "xml" should be also checked (http://www.w3.org/XML/xml-names-19990114-errata#NE05)
45 // but MS.NET ignores such case.
46 if (prefix == "xmlns" || (prefix == "" && localName == "xmlns"))
47 if (namespaceURI != XmlNamespaceManager.XmlnsXmlns)
48 throw new ArgumentException ("Invalid attribute namespace for namespace declaration.");
49 else if (prefix == "xml" && namespaceURI != XmlNamespaceManager.XmlnsXml)
50 throw new ArgumentException ("Invalid attribute namespace for namespace declaration.");
52 // There are no means to identify the DOM is namespace-
53 // aware or not, so we can only check Name validity.
54 if (prefix != "" && !XmlChar.IsName (prefix))
55 throw new ArgumentException ("Invalid attribute prefix.");
56 else if (!XmlChar.IsName (localName))
57 throw new ArgumentException ("Invalid attribute local name.");
60 this.localName = localName;
61 this.namespaceURI = namespaceURI;
68 public override string BaseURI {
70 return OwnerElement.BaseURI;
74 public override string InnerText {
76 return base.InnerText;
84 public override string InnerXml {
86 // Not sure why this is an override. Passing through for now.
92 XmlNamespaceManager nsmgr = ConstructNamespaceManager ();
93 XmlParserContext ctx = new XmlParserContext (OwnerDocument.NameTable, nsmgr,
94 OwnerDocument.DocumentType != null ? OwnerDocument.DocumentType.DTD : null,
95 BaseURI, XmlLang, XmlSpace, null);
96 XmlTextReader xtr = new XmlTextReader (value, XmlNodeType.Attribute, ctx);
97 xtr.XmlResolver = OwnerDocument.Resolver;
99 OwnerDocument.ReadAttributeNodeValue (xtr, this);
103 public override string LocalName {
109 public override string Name {
111 return prefix != String.Empty ? prefix + ":" + localName : localName;
115 public override string NamespaceURI {
121 public override XmlNodeType NodeType {
123 return XmlNodeType.Attribute;
127 internal override XPathNodeType XPathNodeType {
129 return XPathNodeType.Attribute;
133 public override XmlDocument OwnerDocument {
135 return base.OwnerDocument;
139 public virtual XmlElement OwnerElement {
145 public override XmlNode ParentNode {
147 // It always returns null (by specification).
152 // We gotta do more in the set block here
153 // We need to do the proper tests and throw
154 // the correct Exceptions
156 // Wrong cases are: (1)check readonly, (2)check character validity,
157 // (3)check format validity, (4)this is attribute and qualifiedName != "xmlns"
158 public override string Prefix {
161 throw new XmlException ("This node is readonly.");
162 if (!XmlChar.IsNCName (value))
163 throw new ArgumentException ("Specified name is not a valid NCName: " + value);
173 public virtual bool Specified {
179 public override string Value {
181 XmlNode firstChild = FirstChild;
182 if (firstChild == null)
184 return firstChild.Value;
188 XmlNode firstChild = FirstChild;
189 if (firstChild == null)
190 AppendChild (OwnerDocument.CreateTextNode (value));
192 firstChild.Value = value;
196 internal override string XmlLang {
197 get { return OwnerElement.XmlLang; }
200 internal override XmlSpace XmlSpace {
201 get { return OwnerElement.XmlSpace; }
208 public override XmlNode CloneNode (bool deep)
210 XmlNode node = new XmlAttribute (prefix, localName, namespaceURI,
213 foreach (XmlNode child in this.ChildNodes)
214 node.AppendChild (child.CloneNode (deep));
220 internal void SetDefault ()
225 // Parent of XmlAttribute must be null
226 internal void SetOwnerElement (XmlElement el) {
230 public override void WriteContentTo (XmlWriter w)
232 foreach (XmlNode n in ChildNodes)
236 public override void WriteTo (XmlWriter w)
238 w.WriteStartAttribute (prefix, localName, namespaceURI);
240 w.WriteEndAttribute ();
245 internal override XmlLinkedNode LastLinkedChild {
246 get { return lastChild; }
248 set { lastChild = value; }