public void WriteEndDocument() { try { xw.WriteEndDocument(); Assert.Fail("Expected an Exception."); // in .NET 2.0 it is InvalidOperationException. // in .NET 1,1 it is ArgumentException. } catch (Exception) {} }
public static void FragmentTest() { string rwTypeStr = "Text"; ReaderWriterFactory.ReaderWriterType rwType = (ReaderWriterFactory.ReaderWriterType) Enum.Parse(typeof(ReaderWriterFactory.ReaderWriterType), rwTypeStr, true); Encoding encoding = Encoding.GetEncoding("utf-8"); int numberOfNestedFragments = 1; MemoryStream ms1 = new MemoryStream(); MemoryStream ms2 = new MemoryStream(); XmlDictionaryWriter writer1 = (XmlDictionaryWriter)ReaderWriterFactory.CreateXmlWriter(rwType, ms1, encoding); XmlDictionaryWriter writer2 = (XmlDictionaryWriter)ReaderWriterFactory.CreateXmlWriter(rwType, ms2, encoding); Assert.True(FragmentHelper.CanFragment(writer1)); Assert.True(FragmentHelper.CanFragment(writer2)); writer1.WriteStartDocument(); writer2.WriteStartDocument(); writer1.WriteStartElement(ReaderWriterConstants.RootElementName); writer2.WriteStartElement(ReaderWriterConstants.RootElementName); SimulateWriteFragment(writer1, true, numberOfNestedFragments); SimulateWriteFragment(writer2, false, numberOfNestedFragments); writer1.WriteEndElement(); writer2.WriteEndElement(); writer1.WriteEndDocument(); writer2.WriteEndDocument(); writer1.Flush(); writer2.Flush(); byte[] doc1 = ms1.ToArray(); byte[] doc2 = ms2.ToArray(); CompareArrays(doc1, 0, doc2, 0, doc1.Length); }
protected override void OnWriteEndMessage(XmlDictionaryWriter writer) { if (!_messageEncoder._optimizeWriteForUTF8) { writer.WriteEndDocument(); } }
public virtual Task WriteMessageAsync(Message message, Stream stream) { if (message == null) { throw new ArgumentNullException(nameof(message)); } if (stream == null) { throw new ArgumentNullException(nameof(stream)); } ThrowIfMismatchedMessageVersion(message); XmlDictionaryWriter xmlWriter = XmlDictionaryWriter.CreateTextWriter(stream, _writeEncoding, false); if (_optimizeWriteForUtf8) { message.WriteMessage(xmlWriter); } else { xmlWriter.WriteStartDocument(); message.WriteMessage(xmlWriter); xmlWriter.WriteEndDocument(); } xmlWriter.Flush(); xmlWriter.Dispose(); return(Task.CompletedTask); }
public override void WriteMessage(Message message, Stream stream) { if (message == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message")); } if (stream == null) { throw TraceUtility.ThrowHelperError(new ArgumentNullException("stream"), message); } base.ThrowIfMismatchedMessageVersion(message); message.Properties.Encoder = this; XmlDictionaryWriter writer = this.TakeStreamedWriter(stream); if (this.optimizeWriteForUTF8) { message.WriteMessage(writer); } else { writer.WriteStartDocument(); message.WriteMessage(writer); writer.WriteEndDocument(); } writer.Flush(); this.ReturnStreamedWriter(writer); if (MessageLogger.LogMessagesAtTransportLevel) { MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportSend); } }
public static void WriteObjectData(string path) { // Create the object to serialize. Person p = new Person("Lynn", "Tsoflias", 9876); // Create the writer. FileStream fs = new FileStream(path, FileMode.Create); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs); NetDataContractSerializer ser = new NetDataContractSerializer(); // Use the writer to start a document. writer.WriteStartDocument(true); // Use the serializer to write the start of the // object data. Use it again to write the object // data. ser.WriteStartObject(writer, p); ser.WriteObjectContent(writer, p); // Use the serializer to write the end of the // object data. Then use the writer to write the end // of the document. ser.WriteEndObject(writer); writer.WriteEndDocument(); Console.WriteLine("Done"); // Close and release the writer resources. writer.Flush(); fs.Flush(); fs.Close(); }
private void WriteObjectWithInstance(XmlObjectSerializer xm, Person p, string fileName) { // Use either the XmlDataContractSerializer or NetDataContractSerializer, // or any other class that inherits from XmlObjectSerializer to write with. Console.WriteLine(xm.GetType()); using (Stream fs = new FileStream(fileName, FileMode.Create)) { using (XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs)) { //This does not work //xm.WriteObject(writer, p); //This works ////xm.WriteObject(fs, p); // Use the writer to start a document. writer.WriteStartDocument(true); // Use the serializer to write the start, // content, and end data. xm.WriteStartObject(writer, p); xm.WriteObjectContent(writer, p); xm.WriteEndObject(writer); writer.WriteEndDocument(); Console.WriteLine("Done writing {0}", fileName); } }; }
public override async Task WriteMessageAsync(Message message, Stream stream) { if (message == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(message)); } if (stream == null) { throw TraceUtility.ThrowHelperError(new ArgumentNullException(nameof(stream)), message); } ThrowIfMismatchedMessageVersion(message); message.Properties.Encoder = this; XmlDictionaryWriter xmlWriter = TakeStreamedWriter(stream); if (_optimizeWriteForUTF8) { await message.WriteMessageAsync(xmlWriter); } else { xmlWriter.WriteStartDocument(); await message.WriteMessageAsync(xmlWriter); xmlWriter.WriteEndDocument(); } xmlWriter.Flush(); ReturnStreamedWriter(xmlWriter); await stream.FlushAsync(); }
private void WriteMessage(Message message, Stream stream, string startInfo, string boundary, string startUri, bool writeMessageHeaders) { if (message == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message")); } if (stream == null) { throw TraceUtility.ThrowHelperError(new ArgumentNullException("stream"), message); } base.ThrowIfMismatchedMessageVersion(message); message.Properties.Encoder = this; if (MessageLogger.LogMessagesAtTransportLevel) { MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportSend); } XmlDictionaryWriter writer = this.TakeStreamedWriter(stream, startInfo, boundary, startUri, writeMessageHeaders); if (this.writeEncoding.WebName == "utf-8") { message.WriteMessage(writer); } else { writer.WriteStartDocument(); message.WriteMessage(writer); writer.WriteEndDocument(); } writer.Flush(); this.ReturnStreamedWriter(writer); }
protected override void OnWriteBodyContents(XmlDictionaryWriter writer) { writer.WriteStartDocument(); var serializer = new XmlSerializer(_typeToSerialize); serializer.Serialize(writer, _objectToEncode); writer.WriteEndDocument(); }
/// <summary> /// Closes any open elements or attributes and puts the writer back in the Start state. /// </summary> public override void WriteEndDocument() { _innerWriter.WriteEndDocument(); if (_tracingWriter != null) { _tracingWriter.WriteEndDocument(); } }
public void UseCase1() { Console.WriteLine(); MemoryStream ms = new MemoryStream(); XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(ms, null, null); w.WriteStartDocument(true); w.WriteStartElement("root"); w.WriteAttributeString("a", ""); w.WriteComment(""); w.WriteWhitespace(" "); w.WriteStartElement("AAA", "urn:AAA"); w.WriteEndElement(); w.WriteStartElement("ePfix", "AAA", "urn:AAABBB"); w.WriteEndElement(); w.WriteStartElement("AAA"); w.WriteCData("CCC\u3005\u4E00CCC"); w.WriteString("AAA&AAA"); w.WriteRaw("DDD&DDD"); w.WriteCharEntity('\u4E01'); w.WriteComment("COMMENT"); w.WriteEndElement(); w.WriteStartElement("AAA"); w.WriteAttributeString("BBB", "bbb"); // mhm, how namespace URIs are serialized then? w.WriteAttributeString("pfix", "BBB", "urn:bbb", "bbbbb"); // 0x26-0x3F w.WriteAttributeString("CCC", "urn:ccc", "ccccc"); w.WriteAttributeString("DDD", "urn:ddd", "ddddd"); w.WriteAttributeString("CCC", "urn:ddd", "cdcdc"); // XmlLang w.WriteXmlAttribute("lang", "ja"); Assert.AreEqual("ja", w.XmlLang, "XmlLang"); // XmlSpace w.WriteStartAttribute("xml", "space", "http://www.w3.org/XML/1998/namespace"); w.WriteString("pre"); w.WriteString("serve"); w.WriteEndAttribute(); Assert.AreEqual(XmlSpace.Preserve, w.XmlSpace, "XmlSpace"); w.WriteAttributeString("xml", "base", "http://www.w3.org/XML/1998/namespace", "local:hogehoge"); w.WriteString("CCC"); w.WriteBase64(new byte [] { 0x20, 0x20, 0x20, 0xFF, 0x80, 0x30 }, 0, 6); w.WriteEndElement(); // this WriteEndElement() should result in one more // 0x3C, but .net does not output it. w.WriteEndElement(); w.WriteEndDocument(); w.Close(); Assert.AreEqual(usecase1_result, ms.ToArray()); }
public override async Task WriteMessageAsync(Message message, Stream stream) { if (message == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(message))); } if (stream == null) { throw TraceUtility.ThrowHelperError(new ArgumentNullException(nameof(stream)), message); } ThrowIfMismatchedMessageVersion(message); EventTraceActivity eventTraceActivity = null; if (WcfEventSource.Instance.TextMessageEncodingStartIsEnabled()) { eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message); WcfEventSource.Instance.TextMessageEncodingStart(eventTraceActivity); } message.Properties.Encoder = this; XmlDictionaryWriter xmlWriter = TakeStreamedWriter(stream); if (_optimizeWriteForUTF8) { await message.WriteMessageAsync(xmlWriter); } else { xmlWriter.WriteStartDocument(); await message.WriteMessageAsync(xmlWriter); xmlWriter.WriteEndDocument(); } await xmlWriter.FlushAsync(); ReturnStreamedWriter(xmlWriter); if (WcfEventSource.Instance.StreamedMessageWrittenByEncoderIsEnabled()) { WcfEventSource.Instance.StreamedMessageWrittenByEncoder(eventTraceActivity ?? EventTraceActivityHelper.TryExtractActivity(message)); } if (MessageLogger.LogMessagesAtTransportLevel) { MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportSend); } }
private static void Serialize <T>(ref T obj, string fileName, ref Exception ex) { string fullName = string.Format("{0}{1}.xml", ConfigurationManager.AppSettings["SerializeLocation"], fileName); #region Error Xml Format XmlDocument doc = new XmlDocument(); XmlNode ErrorDetailsNode = doc.CreateElement("ErrorDetails"); XmlNode ExceptionTypeNode = doc.CreateElement("ExceptionType"); ExceptionTypeNode.InnerText = ex.GetType().ToString(); ErrorDetailsNode.AppendChild(ExceptionTypeNode); XmlNode MessageNode = doc.CreateElement("Message"); MessageNode.InnerText = ex.Message; ErrorDetailsNode.AppendChild(MessageNode); XmlNode InnerExceptionNode = doc.CreateElement("InnerException"); InnerExceptionNode.InnerText = ex.InnerException != null ? ex.InnerException.Message : ""; ErrorDetailsNode.AppendChild(InnerExceptionNode); XmlNode StackTraceNode = doc.CreateElement("StackTrace"); StackTraceNode.InnerText = ex.StackTrace; ErrorDetailsNode.AppendChild(StackTraceNode); doc.AppendChild(ErrorDetailsNode); #endregion DataContractSerializer s = new DataContractSerializer(typeof(T)); XmlSerializer xs = new XmlSerializer(typeof(XmlDocument)); using (System.IO.FileStream fs = File.Open(fullName, FileMode.Create)) { XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs); writer.WriteStartDocument(true); writer.WriteStartElement("FailedSaveCall"); s.WriteStartObject(writer, obj); s.WriteObjectContent(writer, obj); s.WriteEndObject(writer); xs.Serialize(writer, doc); writer.WriteEndElement(); writer.WriteEndDocument(); } }
void WriteMessage(Message message, Stream stream, string startInfo, string boundary, string startUri, bool writeMessageHeaders) { if (message == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message")); } if (stream == null) { throw TraceUtility.ThrowHelperError(new ArgumentNullException("stream"), message); } ThrowIfMismatchedMessageVersion(message); EventTraceActivity eventTraceActivity = null; if (TD.MtomMessageEncodingStartIsEnabled()) { eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message); TD.MtomMessageEncodingStart(eventTraceActivity); } message.Properties.Encoder = this; if (MessageLogger.LogMessagesAtTransportLevel) { MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportSend); } XmlDictionaryWriter xmlWriter = TakeStreamedWriter(stream, startInfo, boundary, startUri, writeMessageHeaders); if (this.writeEncoding.WebName == "utf-8") { message.WriteMessage(xmlWriter); } else { xmlWriter.WriteStartDocument(); message.WriteMessage(xmlWriter); xmlWriter.WriteEndDocument(); } xmlWriter.Flush(); ReturnStreamedWriter(xmlWriter); if (TD.StreamedMessageWrittenByEncoderIsEnabled()) { TD.StreamedMessageWrittenByEncoder(eventTraceActivity ?? EventTraceActivityHelper.TryExtractActivity(message)); } }
/// <summary> /// Write Message to the string /// </summary> /// <param name="message"></param> /// <returns></returns> public static string WriteToString(this System.ServiceModel.Channels.Message message) { StringBuilder sb = new StringBuilder(); XmlWriterSettings settings = new XmlWriterSettings() { OmitXmlDeclaration = true, Indent = true, Encoding = UTF8Encoding.UTF8 }; using (XmlDictionaryWriter dwr = XmlDictionaryWriter.CreateDictionaryWriter(XmlDictionaryWriter.Create(sb, settings))) { dwr.WriteStartDocument(); message.WriteMessage(dwr); dwr.WriteEndDocument(); dwr.Flush(); } return(sb.ToString()); }
//</snippet6> //<snippet7> public static void WriteObjectContentInDocument(string path) { // Create the object to serialize. Person p = new Person("Lynn", "Tsoflias", 9876); // Create the writer. FileStream fs = new FileStream(path, FileMode.Create); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs); NetDataContractSerializer ser = new NetDataContractSerializer(); // Use the writer to start a document. writer.WriteStartDocument(true); // Use the writer to write the root element. writer.WriteStartElement("Company"); // Use the writer to write an element. writer.WriteElementString("Name", "Microsoft"); // Use the serializer to write the start, content, // and end data. ser.WriteStartObject(writer, p); writer.WriteStartAttribute("localName"); writer.WriteString("My Value"); writer.WriteEndAttribute(); ser.WriteObjectContent(writer, p); ser.WriteEndObject(writer); // Use the writer to write the end element // and end of the document. writer.WriteEndElement(); writer.WriteEndDocument(); // Close and release the writer resources. writer.Flush(); fs.Flush(); fs.Close(); }
private void WriteObjectContentInDocument(XmlObjectSerializer xm, Person p, string fileName) { // Create the writer object. FileStream fs = new FileStream(fileName, FileMode.Create); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs); DataContractSerializer ser = new DataContractSerializer(typeof(Person)); // Use the writer to start a document. writer.WriteStartDocument(true); //// Use the writer to write the root element. //writer.WriteStartElement("Company"); //// Use the writer to write an element. //writer.WriteElementString("Name", "Microsoft"); // Use the serializer to write the start, // content, and end data. xm.WriteStartObject(writer, p); xm.WriteObjectContent(writer, p); xm.WriteEndObject(writer); // Use the writer to write the end element and // the end of the document. writer.WriteEndElement(); writer.WriteEndDocument(); // Close and release the writer resources. writer.Flush(); fs.Flush(); fs.Close(); }
public override void WriteMessage(Message message, Stream stream) { if (message == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message")); } if (stream == null) { throw TraceUtility.ThrowHelperError(new ArgumentNullException("stream"), message); } ThrowIfMismatchedMessageVersion(message); message.Properties.Encoder = this; XmlDictionaryWriter xmlWriter = CreateWriter(stream); xmlWriter.WriteStartDocument(); message.WriteMessage(xmlWriter); xmlWriter.WriteEndDocument(); xmlWriter.Flush(); xmlWriter.Close(); if (MessageLogger.LogMessagesAtTransportLevel) { MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportSend); } }
public override void WriteEndDocument() { _innerWriter.WriteEndDocument(); }
public override void WriteMessage(Message message, Stream stream) { if (message == null) { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("message")); } if (stream == null) { throw TraceUtility.ThrowHelperError(new ArgumentNullException("stream"), message); } ThrowIfMismatchedMessageVersion(message); EventTraceActivity eventTraceActivity = null; if (WebTD.JsonMessageEncodingStartIsEnabled()) { eventTraceActivity = EventTraceActivityHelper.TryExtractActivity(message); WebTD.JsonMessageEncodingStart(eventTraceActivity); } message.Properties.Encoder = this; XmlDictionaryWriter xmlWriter = TakeStreamedWriter(stream); JavascriptCallbackResponseMessageProperty javascriptResponseMessageProperty; if (message.Properties.TryGetValue <JavascriptCallbackResponseMessageProperty>(JavascriptCallbackResponseMessageProperty.Name, out javascriptResponseMessageProperty) && javascriptResponseMessageProperty != null && !String.IsNullOrEmpty(javascriptResponseMessageProperty.CallbackFunctionName)) { if (!this.crossDomainScriptAccessEnabled) { throw TraceUtility.ThrowHelperError(new InvalidOperationException(SR2.JavascriptCallbackNotEnabled), message); } byte[] buffer = this.writeEncoding.GetBytes(String.Format(CultureInfo.InvariantCulture, "{0}(", javascriptResponseMessageProperty.CallbackFunctionName)); stream.Write(buffer, 0, buffer.Length); } xmlWriter.WriteStartDocument(); message.WriteMessage(xmlWriter); xmlWriter.WriteEndDocument(); xmlWriter.Flush(); ReturnStreamedWriter(xmlWriter); if (javascriptResponseMessageProperty != null && !String.IsNullOrEmpty(javascriptResponseMessageProperty.CallbackFunctionName)) { if (javascriptResponseMessageProperty.StatusCode != null && (int)javascriptResponseMessageProperty.StatusCode != 200) { byte[] buffer = this.writeEncoding.GetBytes(String.Format(CultureInfo.InvariantCulture, ",{0}", (int)javascriptResponseMessageProperty.StatusCode)); stream.Write(buffer, 0, buffer.Length); } stream.Write(this.encodedClosingFunctionCall, 0, this.encodedClosingFunctionCall.Length); } if (SMTD.StreamedMessageWrittenByEncoderIsEnabled()) { SMTD.StreamedMessageWrittenByEncoder( eventTraceActivity ?? EventTraceActivityHelper.TryExtractActivity(message)); } if (MessageLogger.LogMessagesAtTransportLevel) { MessageLogger.LogMessage(ref message, MessageLoggingSource.TransportSend); } }
public void WriteEndDocument() { w.WriteEndDocument(); // so, it is completely wrong, but ignored. }
protected override void OnWriteEndMessage(XmlDictionaryWriter writer) { writer.WriteEndDocument(); }