0
|
1 ???using System; |
|
2 using System.Text; |
|
3 using System.IO; |
|
4 using System.Xml; |
|
5 |
|
6 namespace UCIS.Xml { |
|
7 public class XmlSocket : XmlWriter { |
|
8 protected Stream pStream; |
|
9 protected XmlWriter pWriter; |
|
10 protected XmlReaderSettings pXMLReaderSettings;// = new XmlReaderSettings(); |
|
11 |
|
12 public Stream BaseStream { get { return pStream; } } |
|
13 |
|
14 public override XmlWriterSettings Settings { get { return WriterSettings; } } |
|
15 public XmlWriterSettings WriterSettings { get; private set; } |
|
16 public XmlReaderSettings ReaderSettings { get { return pXMLReaderSettings; } } |
|
17 |
|
18 public XmlSocket(Stream Stream, Encoding Encoding) { |
|
19 WriterSettings = new XmlWriterSettings(); |
|
20 WriterSettings.ConformanceLevel = ConformanceLevel.Document; |
|
21 WriterSettings.NewLineHandling = NewLineHandling.Entitize; |
|
22 WriterSettings.OmitXmlDeclaration = true; |
|
23 WriterSettings.CheckCharacters = false; |
|
24 WriterSettings.Encoding = Encoding; |
|
25 WriterSettings.CloseOutput = false; |
|
26 |
|
27 pXMLReaderSettings = new XmlReaderSettings(); |
|
28 pXMLReaderSettings.ConformanceLevel = ConformanceLevel.Document; |
|
29 pXMLReaderSettings.CloseInput = true; |
|
30 pXMLReaderSettings.IgnoreComments = true; |
|
31 pXMLReaderSettings.IgnoreProcessingInstructions = true; |
|
32 pXMLReaderSettings.IgnoreWhitespace = true; |
|
33 pXMLReaderSettings.ValidationType = ValidationType.None; |
|
34 pXMLReaderSettings.ValidationFlags = System.Xml.Schema.XmlSchemaValidationFlags.None; |
|
35 pXMLReaderSettings.CheckCharacters = false; |
|
36 |
|
37 pStream = Stream; |
|
38 } |
|
39 public XmlSocket(Stream Stream) : this(Stream, new UTF8NoPreamble()) { } |
|
40 |
|
41 public virtual MemoryStream ReadRawDocument() { |
|
42 MemoryStream Buffer = new MemoryStream(); |
|
43 byte[] ByteBuffer = new byte[1]; |
|
44 int ByteCount = 0; |
|
45 while (true) { |
|
46 ByteCount = pStream.Read(ByteBuffer, 0, 1); |
|
47 if (ByteCount == 0) { |
|
48 throw new EndOfStreamException(); |
|
49 } else if (ByteBuffer[0] == 0) { |
|
50 if (Buffer.Length > 0) break; |
|
51 } else { |
|
52 Buffer.WriteByte(ByteBuffer[0]); |
|
53 } |
|
54 } |
|
55 Buffer.Flush(); |
|
56 Buffer.Seek(0, SeekOrigin.Begin); |
|
57 |
|
58 return Buffer; |
|
59 } |
|
60 public virtual XmlDocument ReadDocument() { |
|
61 MemoryStream Buffer = ReadRawDocument(); |
|
62 try { |
|
63 XmlDocument Doc = new XmlDocument(); |
|
64 XmlReader XMLReader = XmlReader.Create(Buffer, pXMLReaderSettings); |
|
65 Doc.Load(XMLReader); |
|
66 XMLReader.Close(); |
|
67 return Doc; |
|
68 } catch (Exception ex) { |
|
69 Buffer.Seek(0, SeekOrigin.Begin); |
|
70 throw new IOException("Could not parse XML document: \"" + Encoding.UTF8.GetString(Buffer.ToArray()) + "\"", ex); |
|
71 } |
|
72 } |
|
73 |
|
74 protected virtual void CreateWriter() { |
|
75 pWriter = XmlWriter.Create(pStream, WriterSettings); |
|
76 } |
|
77 protected virtual void CloseWriter() { |
|
78 pWriter.Flush(); |
|
79 pWriter.Close(); |
|
80 pStream.WriteByte(0); |
|
81 pStream.Flush(); |
|
82 } |
|
83 public override void Flush() { |
|
84 if (pWriter != null) { |
|
85 pWriter.Flush(); |
|
86 } else { |
|
87 pStream.Flush(); |
|
88 } |
|
89 } |
|
90 public override void Close() { |
|
91 if (pWriter != null) { |
|
92 pWriter.Close(); |
|
93 pWriter = null; |
|
94 } |
|
95 pStream.Close(); |
|
96 } |
|
97 |
|
98 public void WriteDocument(XmlDocument Document) { |
|
99 CreateWriter(); |
|
100 Document.WriteTo(pWriter); |
|
101 CloseWriter(); |
|
102 } |
|
103 |
|
104 public virtual void WriteRawDocument(string data) { |
|
105 byte[] Buffer = WriterSettings.Encoding.GetBytes(data); |
|
106 pStream.Write(Buffer, 0, Buffer.Length); |
|
107 pStream.WriteByte(0); |
|
108 pStream.Flush(); |
|
109 } |
|
110 |
|
111 public override void WriteStartDocument() { |
|
112 CreateWriter(); |
|
113 pWriter.WriteStartDocument(); |
|
114 } |
|
115 public override void WriteStartDocument(bool standalone) { |
|
116 CreateWriter(); |
|
117 pWriter.WriteStartDocument(standalone); |
|
118 } |
|
119 public override void WriteEndDocument() { |
|
120 pWriter.WriteEndDocument(); |
|
121 CloseWriter(); |
|
122 } |
|
123 public void WriteDocumentString(string localName, string value) { |
|
124 WriteStartDocument(); |
|
125 WriteElementString(localName, value); |
|
126 WriteEndDocument(); |
|
127 } |
|
128 |
|
129 public override System.Xml.WriteState WriteState { |
|
130 get { |
|
131 if (pWriter != null) { |
|
132 return pWriter.WriteState; |
|
133 } else { |
|
134 return WriteState.Start; |
|
135 } |
|
136 } |
|
137 } |
|
138 |
|
139 public override string LookupPrefix(string ns) { |
|
140 return pWriter.LookupPrefix(ns); |
|
141 } |
|
142 public override void WriteBase64(byte[] buffer, int index, int count) { |
|
143 pWriter.WriteBase64(buffer, index, count); |
|
144 } |
|
145 public override void WriteCData(string text) { |
|
146 pWriter.WriteCData(text); |
|
147 } |
|
148 public override void WriteCharEntity(char ch) { |
|
149 pWriter.WriteCharEntity(ch); |
|
150 } |
|
151 public override void WriteChars(char[] buffer, int index, int count) { |
|
152 pWriter.WriteChars(buffer, index, count); |
|
153 } |
|
154 public override void WriteComment(string text) { |
|
155 pWriter.WriteComment(text); |
|
156 } |
|
157 public override void WriteDocType(string name, string pubid, string sysid, string subset) { |
|
158 pWriter.WriteDocType(name, pubid, sysid, subset); |
|
159 } |
|
160 public override void WriteEndAttribute() { |
|
161 pWriter.WriteEndAttribute(); |
|
162 } |
|
163 public override void WriteEndElement() { |
|
164 pWriter.WriteEndElement(); |
|
165 } |
|
166 public override void WriteEntityRef(string name) { |
|
167 pWriter.WriteEntityRef(name); |
|
168 } |
|
169 public override void WriteFullEndElement() { |
|
170 pWriter.WriteFullEndElement(); |
|
171 } |
|
172 public override void WriteProcessingInstruction(string name, string text) { |
|
173 pWriter.WriteProcessingInstruction(name, text); |
|
174 } |
|
175 public override void WriteRaw(char[] buffer, int index, int count) { |
|
176 pWriter.WriteRaw(buffer, index, count); |
|
177 } |
|
178 public override void WriteRaw(string data) { |
|
179 pWriter.WriteRaw(data); |
|
180 } |
|
181 public override void WriteStartAttribute(string prefix, string localName, string ns) { |
|
182 pWriter.WriteStartAttribute(prefix, localName, ns); |
|
183 } |
|
184 public override void WriteStartElement(string prefix, string localName, string ns) { |
|
185 pWriter.WriteStartElement(prefix, localName, ns); |
|
186 } |
|
187 public override void WriteString(string text) { |
|
188 pWriter.WriteString(text); |
|
189 } |
|
190 public override void WriteSurrogateCharEntity(char lowChar, char highChar) { |
|
191 pWriter.WriteSurrogateCharEntity(lowChar, highChar); |
|
192 } |
|
193 public override void WriteWhitespace(string ws) { |
|
194 pWriter.WriteWhitespace(ws); |
|
195 } |
|
196 } |
|
197 } |