2 // System.Xml.XmlDeclaration
5 // Duncan Mak (duncan@ximian.com)
11 using System.Text.RegularExpressions;
15 public class XmlDeclaration : XmlLinkedNode
17 string encoding = "UTF-8"; // defaults to UTF-8
21 protected internal XmlDeclaration (string version, string encoding,
22 string standalone, XmlDocument doc)
28 if (standalone == null)
31 this.version = version;
32 this.encoding = encoding;
33 this.standalone = standalone;
36 public string Encoding {
37 get { return encoding; }
38 set { encoding = (value == null) ? String.Empty : value; }
41 public override string InnerText {
43 set { ParseInput (value); }
46 public override string LocalName {
50 public override string Name {
54 public override XmlNodeType NodeType {
55 get { return XmlNodeType.XmlDeclaration; }
58 public string Standalone {
59 get { return standalone; }
63 if (value.ToUpper() == "YES")
65 if (value.ToUpper() == "NO")
69 standalone = String.Empty;
73 public override string Value {
75 string formatEncoding = "";
76 string formatStandalone = "";
78 if (encoding != String.Empty)
79 formatEncoding = String.Format (" encoding=\"{0}\"", encoding);
81 if (standalone != String.Empty)
82 formatStandalone = String.Format (" standalone=\"{0}\"", standalone);
84 return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
86 set { ParseInput (value); }
89 public string Version {
90 get { return version; }
93 public override XmlNode CloneNode (bool deep)
95 return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
98 public override void WriteContentTo (XmlWriter w) {}
100 public override void WriteTo (XmlWriter w)
102 // This doesn't seem to match up very well with w.WriteStartDocument()
103 // so writing out custom here.
104 w.WriteRaw (String.Format ("<?xml {0}?>", Value));
107 void ParseInput (string input)
109 char [] sep = new char [] {'\'', '"'};
110 if (!input.Trim ().StartsWith ("version"))
111 throw new XmlException("missing \"version\".");
112 int start = input.IndexOf ("encoding");
115 int valStart = input.IndexOfAny (sep, start) + 1;
116 int valEnd = input.IndexOfAny (sep, valStart);
117 Encoding = input.Substring (valStart, valEnd - valStart);
118 sstart = input.IndexOf ("standalone");
121 sstart = input.IndexOf ("standalone");
124 int svalStart = input.IndexOfAny (sep, sstart) + 1;
125 int svalEnd = input.IndexOfAny (sep, svalStart);
126 Standalone = input.Substring (svalStart, svalEnd - svalStart);
127 } // TODO: some error check