1 //------------------------------------------------------------------------------
2 // <copyright file="XmlDeclaration.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
5 // <owner current="true" primary="true">[....]</owner>
6 //------------------------------------------------------------------------------
10 using System.Diagnostics;
12 // Represents the xml declaration nodes: <?xml version='1.0' ...?>
13 public class XmlDeclaration : XmlLinkedNode {
15 const string YES = "yes";
16 const string NO = "no";
18 private string version;
19 private string encoding;
20 private string standalone;
22 protected internal XmlDeclaration( string version, string encoding, string standalone, XmlDocument doc ) : base( doc ) {
23 if ( !IsValidXmlVersion( version ) )
24 throw new ArgumentException( Res.GetString( Res.Xdom_Version ) );
25 if( ( standalone != null ) && ( standalone.Length > 0 ) )
26 if ( ( standalone != YES ) && ( standalone != NO ) )
27 throw new ArgumentException( Res.GetString(Res.Xdom_standalone, standalone) );
28 this.Encoding = encoding;
29 this.Standalone = standalone;
30 this.Version = version;
34 // The version attribute for <?xml version= '1.0' ... ?>
35 public string Version {
36 get { return this.version; }
37 internal set { this.version = value; }
40 // Specifies the value of the encoding attribute, as for
41 // <?xml version= '1.0' encoding= 'UTF-8' ?>
42 public string Encoding {
43 get { return this.encoding; }
44 set { this.encoding = ( (value == null) ? String.Empty : value ); }
47 // Specifies the value of the standalone attribute.
48 public string Standalone {
49 get { return this.standalone; }
52 this.standalone = String.Empty;
53 else if ( value.Length == 0 || value == YES || value == NO )
54 this.standalone = value;
56 throw new ArgumentException( Res.GetString(Res.Xdom_standalone, value) );
60 public override String Value {
61 get { return InnerText; }
62 set { InnerText = value; }
66 // Gets or sets the concatenated values of the node and
68 public override string InnerText {
70 StringBuilder strb = new StringBuilder("version=\"" + Version + "\"");
71 if ( Encoding.Length > 0 ) {
72 strb.Append(" encoding=\"");
73 strb.Append(Encoding);
76 if ( Standalone.Length > 0 ) {
77 strb.Append(" standalone=\"");
78 strb.Append(Standalone);
81 return strb.ToString();
85 string tempVersion = null;
86 string tempEncoding = null;
87 string tempStandalone = null;
88 string orgEncoding = this.Encoding;
89 string orgStandalone = this.Standalone;
90 string orgVersion = this.Version;
92 XmlLoader.ParseXmlDeclarationValue( value, out tempVersion, out tempEncoding, out tempStandalone );
95 if ( tempVersion != null && !IsValidXmlVersion(tempVersion) )
96 throw new ArgumentException(Res.GetString(Res.Xdom_Version));
97 Version = tempVersion;
99 if ( tempEncoding != null )
100 Encoding = tempEncoding;
101 if ( tempStandalone != null )
102 Standalone = tempStandalone;
105 Encoding = orgEncoding;
106 Standalone = orgStandalone;
107 Version = orgVersion;
113 //override methods and properties from XmlNode
115 // Gets the name of the node.
116 public override String Name {
122 // Gets the name of the current node without the namespace prefix.
123 public override string LocalName {
127 // Gets the type of the current node.
128 public override XmlNodeType NodeType {
129 get { return XmlNodeType.XmlDeclaration;}
132 // Creates a duplicate of this node.
133 public override XmlNode CloneNode(bool deep) {
134 Debug.Assert( OwnerDocument != null );
135 return OwnerDocument.CreateXmlDeclaration( Version, Encoding, Standalone );
138 // Saves the node to the specified XmlWriter.
139 public override void WriteTo(XmlWriter w) {
140 w.WriteProcessingInstruction(Name, InnerText);
144 // Saves all the children of the node to the specified XmlWriter.
145 public override void WriteContentTo(XmlWriter w) {
146 // Intentionally do nothing since the node doesn't have children.
149 private bool IsValidXmlVersion(string ver) {
150 return ver.Length >= 3 && ver[0] == '1' && ver[1] == '.' && XmlCharType.IsOnlyDigits(ver, 2, ver.Length - 2);