XmlDeclaration.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // System.Xml.XmlDeclaration
  3. //
  4. // Author:
  5. // Duncan Mak ([email protected])
  6. //
  7. // (C) Ximian, Inc.
  8. using System;
  9. using System.Xml;
  10. namespace System.Xml
  11. {
  12. public class XmlDeclaration : XmlLinkedNode
  13. {
  14. string encoding = "UTF-8"; // defaults to UTF-8
  15. string standalone;
  16. string version;
  17. protected internal XmlDeclaration (string version, string encoding,
  18. string standalone, XmlDocument doc)
  19. : base (doc)
  20. {
  21. if (encoding == null)
  22. encoding = "";
  23. if (standalone == null)
  24. standalone = "";
  25. this.version = version;
  26. this.encoding = encoding;
  27. this.standalone = standalone;
  28. }
  29. public string Encoding {
  30. get { return encoding; }
  31. set { encoding = (value == null) ? String.Empty : value; }
  32. }
  33. public override string InnerText {
  34. get { return Value; }
  35. set { ParseInput (value); }
  36. }
  37. public override string LocalName {
  38. get { return "xml"; }
  39. }
  40. public override string Name {
  41. get { return "xml"; }
  42. }
  43. public override XmlNodeType NodeType {
  44. get { return XmlNodeType.XmlDeclaration; }
  45. }
  46. public string Standalone {
  47. get { return standalone; }
  48. set {
  49. if (value.ToUpper() == "YES")
  50. standalone = "yes";
  51. if (value.ToUpper() == "NO")
  52. standalone = "no";
  53. }
  54. }
  55. public override string Value {
  56. get {
  57. string formatEncoding = "";
  58. string formatStandalone = "";
  59. if (encoding != String.Empty)
  60. formatEncoding = String.Format (" encoding=\"{0}\"", encoding);
  61. if (standalone != String.Empty)
  62. formatStandalone = String.Format (" standalone=\"{0}\"", standalone);
  63. return String.Format ("version=\"{0}\"{1}{2}", Version, formatEncoding, formatStandalone);
  64. }
  65. set { ParseInput (value); }
  66. }
  67. public string Version {
  68. get { return version; }
  69. }
  70. public override XmlNode CloneNode (bool deep)
  71. {
  72. return new XmlDeclaration (Version, Encoding, standalone, OwnerDocument);
  73. }
  74. public override void WriteContentTo (XmlWriter w) {}
  75. public override void WriteTo (XmlWriter w)
  76. {
  77. // This doesn't seem to match up very well with w.WriteStartDocument()
  78. // so writing out custom here.
  79. w.WriteRaw (String.Format ("<?xml {0}?>", Value));
  80. }
  81. void ParseInput (string input)
  82. {
  83. Encoding = input.Split (new char [] { ' ' }) [1].Split (new char [] { '=' }) [1];
  84. Standalone = input.Split (new char [] { ' ' }) [2].Split (new char [] { '=' }) [1];
  85. }
  86. }
  87. }