Example #1
0
 public override string  ToString()
 {
     try
     {
         if (_questionnaire != null)
         {
             Type T = _questionnaire.GetType();
             XmlObjectSerializer serializer = new DataContractSerializer(T, T.ToString(), "", null, 0x7FFF, false, true, null);
             StringWriter        sww        = new StringWriter();
             XmlWriter           writer     = XmlWriter.Create(sww);
             serializer.WriteStartObject(writer, _questionnaire);
             serializer.WriteObjectContent(writer, _questionnaire);
             serializer.WriteEndObject(writer);
             writer.Flush();
             return(sww.ToString());
             // Your xml
         }
         else
         {
             Type T = _questionnaireFormat.GetType();
             XmlObjectSerializer serializer = new DataContractSerializer(T, T.ToString(), "", null, 0x7FFF, false, true, null);
             StringWriter        sww        = new StringWriter();
             XmlWriter           writer     = XmlWriter.Create(sww);
             serializer.WriteStartObject(writer, _questionnaireFormat);
             serializer.WriteObjectContent(writer, _questionnaireFormat);
             serializer.WriteEndObject(writer);
             writer.Flush();
             return(sww.ToString());
         }
     }catch (Exception exc) {
         Form1.Print("This file is not supported");
         return("0");
     }
 }
Example #2
0
        private void Serialize(string path)
        {
            FileStream someStream = new FileStream(path, FileMode.Open);

            //<snippet8>
            Person p = new Person();
            DataContractSerializer dcs =
                new DataContractSerializer(typeof(Person));
            XmlDictionaryWriter xdw =
                XmlDictionaryWriter.CreateTextWriter(someStream, Encoding.UTF8);

            dcs.WriteObject(xdw, p);
            //</snippet8>

            //<snippet9>
            dcs.WriteStartObject(xdw, p);
            xdw.WriteAttributeString("serializedBy", "myCode");
            dcs.WriteObjectContent(xdw, p);
            dcs.WriteEndObject(xdw);
            //</snippet9>

            //<snippet10>
            xdw.WriteStartElement("MyCustomWrapper");
            dcs.WriteObjectContent(xdw, p);
            xdw.WriteEndElement();
            //</snippet10>
        }
        public static void Save(IEnumerable <PokemonTeam> teams)
        {
            Current = teams.ToArray();
            var dir = Path.GetDirectoryName(FileName);

            if (!Directory.Exists(dir))
            {
                Directory.CreateDirectory(dir);
            }
            using (var f = new FileStream(FileName, FileMode.Create, FileAccess.Write))
            {
                var writer = XmlWriter.Create(f, new XmlWriterSettings()
                {
                    CloseOutput        = false,
                    ConformanceLevel   = ConformanceLevel.Fragment,
                    NamespaceHandling  = NamespaceHandling.OmitDuplicates,
                    Indent             = false,
                    OmitXmlDeclaration = true
                });
                var serializer = new DataContractSerializer(Current.GetType());
                serializer.WriteStartObject(writer, Current);
                writer.WriteAttributeString("xmlns", PBOMarks.MS, null, PBOMarks.STANDARD);
                writer.WriteAttributeString("xmlns", PBOMarks.A, null, PBOMarks.ARRAY);
                serializer.WriteObjectContent(writer, Current);
                serializer.WriteEndObject(writer);
                writer.Flush();
            }
        }
Example #4
0
        public string Serialize()
        {
            String result;

            var serializer = new DataContractSerializer(this.GetType());
            var settings   = new XmlWriterSettings
            {
                Indent            = true,
                NamespaceHandling = NamespaceHandling.OmitDuplicates,
                Encoding          = Encoding.UTF8,
            };

            using (var sw = new StringWriter())
            {
                using (var xw = XmlWriter.Create(sw, settings))
                {
                    serializer.WriteStartObject(xw, this);
                    xw.WriteAttributeString("xmlns", "xs", null, "http://www.w3.org/2001/XMLSchema");
                    serializer.WriteObjectContent(xw, this);
                    serializer.WriteEndObject(xw);
                    //serializer.WriteObject(xw, node);
                }
                result = sw.ToString();
            }

            return(result);
        }
Example #5
0
        private static void _WriteSequenceDataToXmlWriter(DataContainer data, XmlWriter xmlWriter)
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(DataContainer), new[] { typeof(AlternatingData), typeof(IModuleDataModel[]), typeof(DataContainer) });

            serializer.WriteStartObject(xmlWriter, data);
            xmlWriter.WriteAttributeString("xmlns", "a", null, "http://www.w3.org/2001/XMLSchema");
            serializer.WriteObjectContent(xmlWriter, data);
            xmlWriter.WriteEndElement();
        }
Example #6
0
        public static void SerializeStartObject <T>(XmlWriter xw, T val)
        {
            DataContractSerializer ser = new DataContractSerializer(typeof(T));

            using (var ms = new MemoryStream())
            {
                ser.WriteStartObject(xw, val);
            }
        }
Example #7
0
        private static void _WriteSequenceDataToXmlWriter(DataContainer data, XmlWriter xmlWriter, Type[] knownTypes)
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(DataContainer), knownTypes);

            serializer.WriteStartObject(xmlWriter, data);
            xmlWriter.WriteAttributeString("xmlns", "a", null, "http://www.w3.org/2001/XMLSchema");
            serializer.WriteObjectContent(xmlWriter, data);
            xmlWriter.WriteEndElement();
        }
Example #8
0
 public static void WriteObject(this DataContractSerializer serializer, XmlWriter stream, Object graph, Dictionary <String, String> namespaces)
 {
     serializer.WriteStartObject(stream, graph);
     foreach (var pair in namespaces)
     {
         stream.WriteAttributeString("xmlns", pair.Key, String.Empty, pair.Value);
     }
     serializer.WriteObjectContent(stream, graph);
     serializer.WriteEndObject(stream);
 }
Example #9
0
 public void SaveSettings(String fileName = "settings.config")
 {
     using (var writer = XmlWriter.Create(fileName, WriterSettings))
     {
         Serializer.WriteStartObject(writer, this);
         writer.WriteAttributeString("xmlns", "a", null, "http://schemas.microsoft.com/2003/10/Serialization/Arrays");
         Serializer.WriteObjectContent(writer, this);
         Serializer.WriteEndObject(writer);
     }
 }
        private void DataContractSerialize(XmlWriterSettings xws, DataContractSerializer dcs, IMessage[] messages, Stream str)
        {
            ArrayList o = new ArrayList(messages);

            using (XmlWriter xwr = XmlWriter.Create(str, xws))
            {
                dcs.WriteStartObject(xwr, o);
                dcs.WriteObjectContent(xwr, o);
                dcs.WriteEndObject(xwr);
            }
        }
Example #11
0
        void DataContractSerialize(XmlWriterSettings xmlWriterSettings, DataContractSerializer dataContractSerializer, IMessage[] messages, Stream stream)
        {
            var o = new ArrayList(messages);

            using (var xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
            {
                dataContractSerializer.WriteStartObject(xmlWriter, o);
                dataContractSerializer.WriteObjectContent(xmlWriter, o);
                dataContractSerializer.WriteEndObject(xmlWriter);
            }
        }
Example #12
0
        public override void SerializeToXml(XmlWriter xw)
        {
            DataContractSerializer ser = new DataContractSerializer(this.GetType());

            using (var ms = new MemoryStream())
            {
                ser.WriteStartObject(xw, this);
                ser.WriteObjectContent(xw, this);
                arrays.SerializeToXml(xw);
                ser.WriteEndObject(xw);
            }
        }
Example #13
0
        public override void SerializeToXml(XmlWriter xw)
        {
            DataContractSerializer ser = new DataContractSerializer(this.GetType());

            using (var ms = new MemoryStream())
            {
                ser.WriteStartObject(xw, this);
                ser.WriteObjectContent(xw, this);
                xw.WriteElementString("val", this.val.Replace("\x00", ""));
                ser.WriteEndObject(xw);
            }
        }
Example #14
0
        public override void SerializeToXml(XmlWriter xw)
        {
            DataContractSerializer ser = new DataContractSerializer(this.GetType());

            using (var ms = new MemoryStream())
            {
                ser.WriteStartObject(xw, this);
                ser.WriteObjectContent(xw, this);
                xw.WriteElementString("PtrTargetType", this.GetPtrTargetType());
                xw.WriteElementString("Target", this.ToString());
                ser.WriteEndObject(xw);
            }
        }
Example #15
0
        private static void XMLSerialize <T>(XmlWriter writer, T data)
        {
            var serializer = new DataContractSerializer(typeof(T));

            serializer.WriteStartObject(writer, data);

            // Optimizing Away Repeat XML Namespace Declarations
            writer.WriteAttributeString("xmlns", "sys", null, "http://www.w3.org/2001/XMLSchema");
            writer.WriteAttributeString("xmlns", "esri", null, "http://schemas.datacontract.org/2004/07/ESRI.ArcGIS.Client.Geometry");
            writer.WriteAttributeString("xmlns", "col", null, "http://schemas.datacontract.org/2004/07/System.Collections.Generic");

            serializer.WriteObjectContent(writer, data);
            serializer.WriteEndObject(writer);
        }
Example #16
0
        public override void SerializeToXml(XmlWriter xw)
        {
            DataContractSerializer ser = new DataContractSerializer(this.GetType());

            using (var ms = new MemoryStream())
            {
                ser.WriteStartObject(xw, this);
                ser.WriteObjectContent(xw, this);
                xw.WriteStartElement("XMLData");
                Data.Save(xw);
                xw.WriteEndElement();
                ser.WriteEndObject(xw);
            }
        }
        private void _WriteSequenceDataToXmlWriter(ISequence sequence, XmlWriter xmlWriter)
        {
            ISequenceTypeModuleInstance sequenceTypeModule = _GetSequenceTypeModule(_fileType);
            DataContractSerializer      serializer         = SequenceTypeService.GetSequenceTypeDataSerializer(sequenceTypeModule);

            if (serializer == null)
            {
                throw new Exception(string.Format("Can't save sequence {0}, no serializer present. ", sequence.Name));
            }

            serializer.WriteStartObject(xmlWriter, sequence.SequenceData);
            _WriteKnownNamespaces(xmlWriter);
            serializer.WriteObjectContent(xmlWriter, sequence.SequenceData);
            serializer.WriteEndObject(xmlWriter);
        }
        public byte[] GetByteArray(string body)
        {
            MemoryStream           stream = new MemoryStream();
            DataContractSerializer s      = new DataContractSerializer(typeof(string));
            XmlDictionaryWriter    writer = XmlDictionaryWriter.CreateBinaryWriter(stream);

            writer.WriteStartDocument();
            s.WriteStartObject(writer, body);
            s.WriteObjectContent(writer, body);
            s.WriteEndObject(writer);
            writer.Flush();
            stream.Position = 0;

            return(stream.ToArray());
        }
Example #19
0
        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();
            }
        }
Example #20
0
        public override void SerializeToXml(XmlWriter xw)
        {
            DataContractSerializer ser = new DataContractSerializer(this.GetType());

            using (var ms = new MemoryStream())
            {
                ser.WriteStartObject(xw, this);
                ser.WriteObjectContent(xw, this);
                xw.WriteElementString("Length", Bytes.Length.ToString());
                if (Bytes.Length > 0)
                {
                    xw.WriteElementString("Bytes", HexStr(Bytes));
                }
                ser.WriteEndObject(xw);
            }
        }
 public static void WriteObject(
     this DataContractSerializer serializer,
     Stream stream, object data,
     Dictionary <string, string> namespaces)
 {
     using (var writer = XmlWriter.Create(stream))
     {
         serializer.WriteStartObject(writer, data);
         foreach (var pair in namespaces)
         {
             writer.WriteAttributeString("xmlns", pair.Key, null, pair.Value);
         }
         serializer.WriteObjectContent(writer, data);
         serializer.WriteEndObject(writer);
     }
 }
Example #22
0
        public DataContract(Type contractType)
        {
            _serializer = new DataContractSerializer(contractType);

            object o;

            if (contractType.IsArray)
            {
                o = Array.CreateInstance(contractType.GetElementType(), 0);
            }
            else
            {
                o = Activator.CreateInstance(contractType, true);
            }


            _serializer.WriteStartObject(this, o);
        }
Example #23
0
        public void SaveXml(string filePath)
        {
            using (XmlTextWriter writer = new XmlTextWriter(filePath, Encoding.UTF8))
            {
                writer.Formatting = Formatting.Indented;

                DataContractSerializer ser = new DataContractSerializer(typeof(AgriCoop));

                // Using references for Machine and Producer means that the Id attribute is used, which is defined in the MS Serialization namespace
                // To get cleaner XML, we declare this namespace first.
                // ser.WriteObject(writer, this); // replace by following lines.
                ser.WriteStartObject(writer, this);
                writer.WriteAttributeString("xmlns", "z", null, "http://schemas.microsoft.com/2003/10/Serialization/");
                ser.WriteObjectContent(writer, this);
                ser.WriteEndObject(writer);

                writer.Close();
            }
        }
Example #24
0
        public virtual void SerializeToXml(XmlWriter xw)
        {
            DataContractSerializer ser = new DataContractSerializer(this.GetType());

            using (var ms = new MemoryStream())
            {
                ser.WriteStartObject(xw, this);
                ser.WriteObjectContent(xw, this);

                if (GetEditableVariables() != null)
                {
                    foreach (var v in GetEditableVariables())
                    {
                        v.SerializeToXml(xw);
                    }
                }
                ser.WriteEndObject(xw);
            }
        }
Example #25
0
        public override void SerializeToXml(XmlWriter xw)
        {
            DataContractSerializer ser = new DataContractSerializer(this.GetType());

            using (var ms = new MemoryStream())
            {
                ser.WriteStartObject(xw, this);
                ser.WriteObjectContent(xw, this);
            }

            if (GetEditableVariables() != null)
            {
                xw.WriteStartElement("array");
                foreach (CVariable v in GetEditableVariables())
                {
                    v.SerializeToXml(xw);
                }
                xw.WriteEndElement();
            }
            ser.WriteEndObject(xw);
        }
        public async Task PostAsync(String data)
        {
            MemoryStream           contentStream = new MemoryStream();
            DataContractSerializer serializer    = new DataContractSerializer(typeof(String));
            XmlDictionaryWriter    writer        = XmlDictionaryWriter.CreateBinaryWriter(contentStream);

            writer.WriteStartDocument();
            serializer.WriteStartObject(writer, data);
            serializer.WriteObjectContent(writer, data);
            serializer.WriteEndObject(writer);
            writer.Flush();

            String fullAddress = "https://darkbird.servicebus.windows.net/checkpointtouches/messages";

            this.client.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "application/atom+xml;type=entry;charset=utf-8");
            this.client.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", GetSASToken(this.url, "RootManageSharedAccessKey", this.sasKey));
            this.client.DefaultRequestHeaders.TryAddWithoutValidation("BrokerProperties", @"{ ""MessageId"": """ + Guid.NewGuid().ToString() + @"""}");
            var result = await this.client.PostAsync(fullAddress, new StreamContent(new System.IO.MemoryStream(contentStream.ToArray())));

            System.Diagnostics.Debug.WriteLine(result.StatusCode.ToString());
        }
Example #27
0
        public static void WriteObject(this DataContractSerializer serializer, XmlWriter stream, object data,
                                       Dictionary <string, string> namespaces)
        {
            XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();

            xmlWriterSettings.Indent = true;
            xmlWriterSettings.NewLineOnAttributes = true;
            xmlWriterSettings.OmitXmlDeclaration  = true;
            xmlWriterSettings.NamespaceHandling   = NamespaceHandling.Default;

            using (var writer = XmlWriter.Create(stream, xmlWriterSettings))
            {
                serializer.WriteStartObject(writer, data);
                foreach (var pair in namespaces)
                {
                    writer.WriteAttributeString("xmlns", pair.Key, null, pair.Value);
                }

                serializer.WriteObjectContent(writer, data);
                serializer.WriteEndObject(writer);
            }
        }
Example #28
0
        //</snippet6>

        //<snippet7>
        public static void WriteObjectContentInDocument(string path)
        {
            // Create the object to serialize.
            Person p = new Person("Lynn", "Tsoflias", 9876);

            // Create the writer object.
            FileStream          fs     = new FileStream(path, 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.
            ser.WriteStartObject(writer, p);
            ser.WriteObjectContent(writer, p);
            ser.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();
        }
Example #29
0
        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);

            DataContractSerializer ser =
                new DataContractSerializer(typeof(Person));

            // 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 writer to add an XML element to the document.
            writer.WriteElementString("Citizen", "true");

            // 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();
        }
Example #30
0
        public void WriteConstraints(string fileName)
        {
            object[] attributes;
            attributes = this.GetType().GetCustomAttributes(typeof(NamespaceAttribute), true);

            var ds = new DataContractSerializer(typeof(SkeletonConstraintsCollection));

            var settings = new XmlWriterSettings {
                Indent = true, NewLineOnAttributes = true
            };

            using (var w = XmlWriter.Create(fileName, settings))
            {
                ds.WriteStartObject(w, this);
                foreach (NamespaceAttribute ns in attributes)
                {
                    w.WriteAttributeString("xmlns", ns.Prefix, null, ns.Uri);
                }

                // content
                ds.WriteObjectContent(w, this);
                ds.WriteEndObject(w);
            }
        }