public void TestWriteStartHeaderWithActor() { Message m = Message.CreateMessage(MessageVersion.Default, "test", 1); MessageHeader h = MessageHeader.CreateHeader("FirstHeader", "ns", "first", true, "actor"); m.Headers.Add(h); StringBuilder sb = new StringBuilder(); XmlWriterSettings s = new XmlWriterSettings(); s.ConformanceLevel = ConformanceLevel.Fragment; XmlWriter w = XmlWriter.Create(sb, s); XmlDictionaryWriter dw = XmlDictionaryWriter.CreateDictionaryWriter(w); m.Headers.WriteStartHeader(1, dw); dw.Close(); w.Close(); string actual = sb.ToString(); sb = new StringBuilder(); w = XmlWriter.Create(sb, s); dw = XmlDictionaryWriter.CreateDictionaryWriter(w); h.WriteStartHeader(dw, MessageVersion.Soap12WSAddressing10); dw.Close(); w.Close(); string expected = sb.ToString(); Assert.AreEqual(expected, actual); }
public void WriteXmlnsAttributeWithNullPrefix() { MemoryStream ms = new MemoryStream(); XmlDictionary dic = new XmlDictionary(); XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(ms, dic, null); w.WriteStartElement("root", "aaaaa"); w.WriteXmlnsAttribute(null, "bbbbb"); w.WriteStartElement("child", "ccccc"); w.WriteXmlnsAttribute(null, "ddddd"); w.WriteEndElement(); w.WriteEndElement(); w.Close(); byte [] bytes = new byte [] { // 0x40 (root) 0x8 (aaaaa) 0x40, 0x4, 0x72, 0x6F, 0x6F, 0x74, 0x8, 0x5, 0x61, 0x61, 0x61, 0x61, 0x61, // 0x9 (a) (bbbbb) 0x9, 0x1, 0x61, 0x5, 0x62, 0x62, 0x62, 0x62, 0x62, // 0x40 (child) 0x8 (ccccc) 0x40, 0x5, 0x63, 0x68, 0x69, 0x6C, 0x64, 0x8, 0x5, 0x63, 0x63, 0x63, 0x63, 0x63, // 0x9 (b) (ddddd) 0x1 0x1 0x9, 0x1, 0x62, 0x5, 0x64, 0x64, 0x64, 0x64, 0x64, 0x1, 0x1 }; Assert.AreEqual(bytes, ms.ToArray()); }
public static Exception serializeDataContract <T>(T obj, string path, DataContractSerializerSettings sett = null) { FileStream fs; try { fs = new FileStream(path, FileMode.Create); } catch (Exception ex) { return(ex); } XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs); try { DataContractSerializer serializer = sett == null ? new DataContractSerializer(typeof(T)) : new DataContractSerializer(typeof(T), sett); serializer.WriteObject(writer, obj); } catch (Exception ex) { return(ex); } finally { writer.Close(); } return(null); }
public void WriteTypedValues() { MemoryStream ms = new MemoryStream(); var dic = new XmlDictionary(); XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(ms, dic, null); w.WriteStartElement("root"); w.WriteXmlnsAttribute("x", "urn:x"); w.WriteXmlnsAttribute("xx", "urn:xx"); w.WriteValue((byte)5); w.WriteValue((short)893); w.WriteValue((int)37564); w.WriteValue((long)141421356); w.WriteValue((long)5670000000); w.WriteValue((float)1.7320508); w.WriteValue((double)2.2360679); w.WriteValue((decimal)3.141592); w.WriteValue(new DateTime(2000, 1, 2, 3, 4, 5)); w.WriteValue(new XmlDictionary().Add("xxx")); // from different dictionary -> string output, not index output // w.WriteValue ((object) null); ANE w.WriteValue(1); w.WriteQualifiedName(dic.Add("local"), dic.Add("urn:x")); w.WriteQualifiedName(dic.Add("local"), dic.Add("urn:xx")); // QName tag is not used, since the prefix is more than 1 byte w.Close(); Assert.AreEqual(typed_values, ms.ToArray()); }
public static void SaveVersionsSettings() { string fullPath = GlobalSettings.MainDirectory + "\\" + Constants.FILENAME_SETTINGS_VERSIONS; string folderPath = fullPath.Substring(0, fullPath.LastIndexOf('\\')); if (!Directory.Exists(folderPath)) { Directory.CreateDirectory(folderPath); } try { using (FileStream fileStream = new FileStream(fullPath, FileMode.Create, FileAccess.Write, FileShare.None)) { using (XmlDictionaryWriter dictionaryWriter = XmlDictionaryWriter.CreateTextWriter(fileStream, Encoding.UTF8, false)) { xmlSerializer.WriteObject(dictionaryWriter, xmlVersionsSettings); dictionaryWriter.Flush(); dictionaryWriter.Close(); } fileStream.Flush(); fileStream.Close(); } } catch (Exception ex) { MessageBox.Show(Messages.ERROR_SAVE_SETTINGS_VERSION + ex.Message, Messages.CAPTION_COMMON); } }
// binary serializing public static T DataContractDeepClone <T>(this T serializableObject) { T copy = default(T); if (serializableObject == null) { return(copy); } using (MemoryStream stream = new MemoryStream()) { DataContractSerializer serializer = new DataContractSerializer(serializableObject.GetType()); using (XmlDictionaryWriter binaryDictionaryWriter = XmlDictionaryWriter.CreateBinaryWriter(stream)) { serializer.WriteObject(binaryDictionaryWriter, serializableObject); binaryDictionaryWriter.Flush(); stream.Seek(0, SeekOrigin.Begin); using (XmlDictionaryReader binaryDictionaryReader = XmlDictionaryReader.CreateBinaryReader(stream, XmlDictionaryReaderQuotas.Max)) { copy = (T)serializer.ReadObject(binaryDictionaryReader); binaryDictionaryReader.Close(); } binaryDictionaryWriter.Close(); } stream.Close(); } return(copy); }
// serialize the object exercise into XML file by fileName public static void WriteExerciseToXml(AbstractExercise exercise, string fileName) { if (exercise == null || fileName == null) { throw new System.ArgumentNullException(); } // open the XML file in FileStream // overwrite the file if it exists, otherwise create a new one FileStream xmlFile = new FileStream(fileName, FileMode.Create); // create a text XML writer // associated it with the opened XML file stream above XmlDictionaryWriter xmlWriter = XmlDictionaryWriter.CreateTextWriter(xmlFile); // create a serializer for the AbstractExercise type DataContractSerializer serializer = new DataContractSerializer(typeof(AbstractExercise)); // serialize the exercise object serializer.WriteObject(xmlWriter, exercise); // close streams xmlWriter.Close(); xmlFile.Close(); }
public static void SnippetReadFrom2() { AddressHeader addressHeader1 = AddressHeader.CreateAddressHeader("specialservice1", "http://localhost:8000/service", 1); AddressHeader addressHeader2 = AddressHeader.CreateAddressHeader("specialservice2", "http://localhost:8000/service", 2); AddressHeader[] addressHeaders = new AddressHeader[2] { addressHeader1, addressHeader2 }; AddressHeaderCollection headers = new AddressHeaderCollection(addressHeaders); EndpointAddress endpointAddress = new EndpointAddress( new Uri("http://localhost:8003/servicemodelsamples/service/incode/identity"), addressHeaders); XmlWriter writer = XmlWriter.Create("addressdata.xml"); XmlDictionaryWriter dictWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer); endpointAddress.WriteTo(AddressingVersion.WSAddressing10, dictWriter); dictWriter.Close(); // <Snippet25> XmlReader reader = XmlReader.Create("addressdata.xml"); XmlDictionaryReader dictReader = XmlDictionaryReader.CreateDictionaryReader(reader); EndpointAddress createdEA = EndpointAddress.ReadFrom (AddressingVersion.WSAddressing10, dictReader); // </Snippet25> }
private static void WriteTraceData(TraceEventType eventType, int id, string traceIdentifier, string description, string methodName, Exception exception) { if (source.Switch.ShouldTrace(eventType)) { MemoryStream strm = new MemoryStream(); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(strm); writer.WriteStartElement(TraceRecordName); writer.WriteAttributeString("xmlns", TraceRecordNs); writer.WriteElementString("TraceIdentifier", traceIdentifier); writer.WriteElementString("Description", description); writer.WriteElementString("AppDomain", AppDomain.CurrentDomain.FriendlyName); writer.WriteElementString("MethodName", methodName); writer.WriteElementString("Source", TraceHelper.TraceSourceName); if (exception != null) { WriteException(writer, exception); } writer.WriteEndElement(); writer.Flush(); strm.Position = 0; XPathDocument doc = new XPathDocument(strm); source.TraceData(eventType, id, doc.CreateNavigator()); writer.Close(); strm.Close(); } }
public static void SnippetReadFrom4() { AddressHeader addressHeader1 = AddressHeader.CreateAddressHeader("specialservice1", "http://localhost:8000/service", 1); AddressHeader addressHeader2 = AddressHeader.CreateAddressHeader("specialservice2", "http://localhost:8000/service", 2); AddressHeader[] addressHeaders = new AddressHeader[2] { addressHeader1, addressHeader2 }; AddressHeaderCollection headers = new AddressHeaderCollection(addressHeaders); EndpointAddress endpointAddress = new EndpointAddress( new Uri("http://localhost:8003/servicemodelsamples/service/incode/identity"), addressHeaders); XmlWriter writer = XmlWriter.Create("addressdata.xml"); XmlDictionaryWriter dictWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer); endpointAddress.WriteTo(AddressingVersion.WSAddressing10, dictWriter); dictWriter.Close(); // <Snippet26> XmlReader reader = XmlReader.Create("addressdata.xml"); XmlDictionaryReader xReader = XmlDictionaryReader.CreateDictionaryReader(reader); // Create an XmlDictionary and add values to it. XmlDictionary d = new XmlDictionary(); XmlDictionaryString xdLocalName = new XmlDictionaryString(XmlDictionary.Empty, "EndpointReference", 0); XmlDictionaryString xdNamespace = new XmlDictionaryString(XmlDictionary.Empty, "http://www.w3.org/2005/08/addressing", 0); EndpointAddress createdEA = EndpointAddress.ReadFrom (AddressingVersion.WSAddressing10, xReader, xdLocalName, xdNamespace ); // </Snippet26> }
public void Beyond128DictionaryEntries () { XmlDictionaryString ds; MemoryStream ms = new MemoryStream (); XmlDictionary dic = new XmlDictionary (); for (int i = 0; i < 260; i++) Assert.AreEqual (i, dic.Add ("n" + i).Key, "d"); XmlDictionary dic2 = new XmlDictionary (); XmlBinaryWriterSession session = new XmlBinaryWriterSession (); int idx; for (int i = 0; i < 260; i++) session.TryAdd (dic2.Add ("n" + i), out idx); XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter (ms, dic, session); w.WriteStartElement (dic.Add ("n128"), dic.Add ("n129")); w.WriteStartElement (dic2.Add ("n130"), dic2.Add ("n131")); w.WriteStartElement (dic.Add ("n132"), dic2.Add ("n133")); w.WriteStartElement (dic.Add ("n256"), dic2.Add ("n256")); w.Close (); byte [] bytes = new byte [] { // so, when it went beyond 128, the index // becomes 2 bytes, where // - the first byte always becomes > 80, and // - the second byte becomes (n / 0x80) * 2. 0x42, 0x80, 2, 0x0A, 0x82, 2, 0x42, 0x85, 2, 0x0A, 0x87, 2, 0x42, 0x88, 2, 0x0A, 0x8B, 2, 0x42, 0x80, 4, 0x0A, 0x81, 4, 1, 1, 1, 1 }; Assert.AreEqual (bytes, ms.ToArray (), "result"); }
public static void GetNonAtomizedNamesTest() { string localNameTest = "localNameTest"; string namespaceUriTest = "http://www.msn.com/"; var encoding = Encoding.UTF8; var rndGen = new Random(); int byteArrayLength = rndGen.Next(100, 2000); byte[] byteArray = new byte[byteArrayLength]; rndGen.NextBytes(byteArray); MemoryStream ms = new MemoryStream(); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(ms, encoding); writer.WriteElementString(localNameTest, namespaceUriTest, "value"); writer.Flush(); ms.Position = 0; XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(ms, encoding, XmlDictionaryReaderQuotas.Max, null); bool success = reader.ReadToDescendant(localNameTest); Assert.True(success); string localName; string namespaceUriStr; reader.GetNonAtomizedNames(out localName, out namespaceUriStr); Assert.Equal(localNameTest, localName); Assert.Equal(namespaceUriTest, namespaceUriStr); writer.Close(); }
public void TestWriteMessage() { Message m = Message.CreateMessage(MessageVersion.Default, "action", 1); Message n = Message.CreateMessage(MessageVersion.Default, "action", 1); Assert.AreEqual(m.ToString(), n.ToString()); // Write out a message using a Binary XmlDictionaryWriter MemoryStream ms = new MemoryStream(); XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(ms); m.WriteMessage(w); w.Close(); ms.Close(); byte [] expected = ms.GetBuffer(); StringBuilder sb = new StringBuilder(); sb.AppendLine("expected: " + expected.Length); for (int i = 0; i < 24; i++) { byte b = expected [i]; sb.Append(String.Format("{0:X02} ", b)); } string exp = sb.ToString(); // Write out an equivalent MessageBuffer MessageBuffer mb = n.CreateBufferedCopy(1024); sb = new StringBuilder(); ms = new MemoryStream(); mb.WriteMessage(ms); ms.Close(); byte [] actual = ms.GetBuffer(); sb.AppendLine("actual: " + actual.Length); for (int i = 0; i < 24; i++) { byte b = actual [i]; sb.Append(String.Format("{0:X02} ", b)); } string act = sb.ToString(); // Console.WriteLine (exp + "\n" + act); // So the lengths of the buffer are the same.... Assert.AreEqual(expected.Length, actual.Length); // TODO: // There are three possible XmlDictionaryWriters: // Binary, Dictionary, Mtom // // It doesn't have a MIME type header, so it's definitely not Mtom. // Dictionary outputs text, so it's not that either. // It's got to be the Binary one. // // But the content differs, why? // FIXME: we don't have AreNotEqual // Assert.AreNotEqual (expected, actual); }
public void WriteElementWithNS() { byte [] bytes = new byte [] { 0x42, 0, 10, 2, 0x98, 3, 0x61, 0x61, 0x61, 0x42, 0, 0x42, 2, 1, 1, 1 }; XmlDictionaryString ds; MemoryStream ms = new MemoryStream(); XmlDictionary dic = new XmlDictionary(); XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(ms, dic, null); w.WriteStartElement(dic.Add("FOO"), dic.Add("foo")); Assert.IsTrue(dic.TryLookup("foo", out ds), "#1"); Assert.AreEqual(1, ds.Key, "#2"); w.WriteString("aaa"); w.WriteStartElement(dic.Add("FOO"), dic.Add("foo")); w.WriteStartElement(dic.Add("foo"), dic.Add("foo")); w.Close(); Assert.AreEqual(bytes, ms.ToArray(), "result"); /* * byte [] bytes2 = new byte [] { * 0x42, 1, 10, 2, 0x98, 3, 0x61, 0x61, * 0x61, 0x42, 0, 0x42, 2, 1, 1, 1}; * * XmlDictionaryReader dr = XmlDictionaryReader.CreateBinaryReader (new MemoryStream (bytes2), dic, new XmlDictionaryReaderQuotas ()); * try { * dr.Read (); * Assert.Fail ("dictionary index 1 should be regarded as invalid."); * } catch (XmlException) { * } */ }
public void UseStandardSession2() { MemoryStream ms = new MemoryStream(); XmlBinaryWriterSession session = new XmlBinaryWriterSession(); XmlDictionary dic = new XmlDictionary(); XmlDictionaryString x = dic.Add("urn:foo"); XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(ms, dic, session); w.WriteStartElement(dic.Add("FOO"), x); w.WriteStartElement(dic.Add("blah"), x); w.WriteStartElement(dic.Add("blabla"), x); w.Close(); Assert.AreEqual(new byte [] { 0x42, 2, 0x0A, 0, 0x42, 4, 0x42, 6, 1, 1, 1 }, ms.ToArray(), "#1"); XmlDictionaryString ds; Assert.IsTrue(dic.TryLookup(0, out ds), "#2-1"); Assert.AreEqual("urn:foo", ds.Value, "#2-2"); Assert.AreEqual(0, ds.Key, "#2-3"); Assert.IsTrue(dic.TryLookup(1, out ds), "#3-1"); Assert.AreEqual("FOO", ds.Value, "#3-2"); Assert.AreEqual(1, ds.Key, "#3-3"); Assert.IsTrue(dic.TryLookup(2, out ds), "#4-1"); Assert.AreEqual("blah", ds.Value, "#4-2"); Assert.AreEqual(2, ds.Key, "#4-3"); Assert.IsTrue(dic.TryLookup(3, out ds), "#5-1"); Assert.AreEqual("blabla", ds.Value, "#5-2"); Assert.AreEqual(3, ds.Key, "#5-3"); }
public void CloseOpenElements() { xw.WriteStartElement("foo"); xw.WriteStartElement("bar"); xw.WriteStartElement("baz"); xw.Close(); Assert.AreEqual("<foo><bar><baz /></bar></foo>", Output, "Close didn't write out end elements properly."); }
public void WriteTo(Stream canonicalStream) { if (this.reader != null) { XmlDictionaryReader dicReader = this.reader as XmlDictionaryReader; if ((dicReader != null) && (dicReader.CanCanonicalize)) { dicReader.MoveToContent(); dicReader.StartCanonicalization(canonicalStream, this.includeComments, this.inclusivePrefixes); dicReader.Skip(); dicReader.EndCanonicalization(); } else { XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(Stream.Null); if (this.inclusivePrefixes != null) { // Add a dummy element at the top and populate the namespace // declaration of all the inclusive prefixes. writer.WriteStartElement("a", reader.LookupNamespace(String.Empty)); for (int i = 0; i < this.inclusivePrefixes.Length; ++i) { string ns = reader.LookupNamespace(this.inclusivePrefixes[i]); if (ns != null) { writer.WriteXmlnsAttribute(this.inclusivePrefixes[i], ns); } } } writer.StartCanonicalization(canonicalStream, this.includeComments, this.inclusivePrefixes); if (reader is WrappedReader) { ((WrappedReader)reader).XmlTokens.GetWriter().WriteTo(writer, new DictionaryManager()); } else { writer.WriteNode(reader, false); } writer.Flush(); writer.EndCanonicalization(); if (this.inclusivePrefixes != null) { writer.WriteEndElement(); } writer.Close(); } if (this.closeReadersAfterProcessing) { this.reader.Close(); } this.reader = null; } else { throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.NoInputIsSetForCanonicalization))); } }
public void save(Stream s) { XmlDictionaryWriter xdw = XmlDictionaryWriter.CreateTextWriter(s, Encoding.UTF8); xdw.WriteStartDocument(); dcs.WriteObject(xdw, this); xdw.Close(); }
protected override void ReturnXmlWriter(XmlDictionaryWriter writer) { writer.Close(); if (this.messageEncoder.optimizeWriteForUTF8 && (this.writer == null)) { this.writer = writer; } }
protected override void ReturnXmlWriter(XmlDictionaryWriter writer) { writer.Close(); if (this.writer == null) { this.writer = writer; } }
/// <summary> /// Closes the underlying stream. /// </summary> public override void Close() { _innerWriter.Close(); if (_tracingWriter != null) { _tracingWriter.Close(); } }
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 void WriteDictionaryStringWithSameDictionary () { MemoryStream ms = new MemoryStream (); XmlDictionary dic = new XmlDictionary (); XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter (ms, dic, null); w.WriteStartElement (dic.Add ("FOO"), XmlDictionaryString.Empty); w.Close (); Assert.AreEqual (new byte [] {0x42, 0, 1}, ms.ToArray ()); }
static void Serialize7(Root r, Stream fs, Stopwatch sw) { NetDataContractSerializer ser = new NetDataContractSerializer(); XmlDictionaryWriter xw = XmlDictionaryWriter.CreateTextWriter(fs); sw.Start(); ser.WriteObject(xw, r); sw.Stop(); xw.Close(); }
static void Serialize6(Root r, Stream fs, Stopwatch sw) { DataContractSerializer ser = new DataContractSerializer(typeof(Root)); XmlDictionaryWriter xw = XmlDictionaryWriter.CreateBinaryWriter(fs); sw.Start(); ser.WriteObject(xw, r); sw.Stop(); xw.Close(); }
public void WriteDictionaryStringWithDifferentDictionary () // it actually works { MemoryStream ms = new MemoryStream (); XmlBinaryWriterSession session = new XmlBinaryWriterSession (); XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter (ms, new XmlDictionary (), session); XmlDictionary dic = new XmlDictionary (); w.WriteStartElement (dic.Add ("FOO"), XmlDictionaryString.Empty); w.Close (); Assert.AreEqual (new byte [] {0x42, 1, 1}, ms.ToArray ()); }
/// <summary> /// <see cref="ISerialize.Save{T}(string, List{T})"/> /// </summary> public void Save <T>(string path, List <T> entities) { using (FileStream fs = new FileStream(path + ".xml", FileMode.Create)) { XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs); DataContractSerializer serializator = new DataContractSerializer(typeof(List <T>)); serializator.WriteObject(writer, entities); writer.Close(); } }
///<summary>Serialze the specified entity, save as file on path</summary> public static void SerialzeEntity <T>(T toSerialize, string path) { FileStream fs = new FileStream(path, FileMode.Create); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs); DataContractSerializer ser = new DataContractSerializer(typeof(T)); ser.WriteObject(writer, toSerialize); writer.Close(); fs.Close(); }
/// <summary> /// Serialize an Object /// </summary> /// <typeparam name="T">Type of object to serialize</typeparam> /// <param name="path">Path of the file that should be serialized to</param> /// <param name="obj">The object to serialize</param> public static void SerializeObject <T>(string path, T obj) { FileStream fs = new FileStream(path, FileMode.Create); XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(fs); DataContractSerializer ser = new DataContractSerializer(typeof(T)); //Serialize the data to a file ser.WriteObject(writer, obj); writer.Close(); }
public void GlobalAttributes() { XmlDictionaryString ds; MemoryStream ms = new MemoryStream(); XmlDictionary dic = new XmlDictionary(); int idx; XmlDictionaryWriter w = XmlDictionaryWriter.CreateBinaryWriter(ms, dic, null); dic.Add("n1"); dic.Add("urn:foo"); dic.Add("n2"); dic.Add("n3"); dic.Add("n4"); dic.Add("urn:bar"); dic.Add("n7"); w.WriteStartElement(dic.Add("n1"), dic.Add("urn:foo")); w.WriteAttributeString(dic.Add("n2"), dic.Add("urn:bar"), String.Empty); w.WriteAttributeString(dic.Add("n3"), dic.Add("urn:foo"), "v"); w.WriteAttributeString("aaa", dic.Add("n4"), dic.Add("urn:bar"), String.Empty); w.WriteAttributeString("bbb", "n5", "urn:foo", String.Empty); w.WriteAttributeString("n6", String.Empty); w.WriteAttributeString(dic.Add("n7"), XmlDictionaryString.Empty, String.Empty); // local attribute w.WriteAttributeString("bbb", "n8", "urn:foo", String.Empty); // xmlns:bbb mapping already exists (from StartElement = 'a'), so prefix "bbb" won't be used. w.Close(); // 0x0C nameidx (value) 0x0D nameidx (value) // 0x07 (prefix) nameidx (value) // 0x05 (prefix) (name) (value) // 0x04... 0x06... 0x05... // 0x0A nsidx // 0x0B (prefix) nsidx // 0x0B... 0x0B... // 0x09 (prefix) (ns) byte [] bytes = new byte [] { // $@$@$$@$ !v$!aaa@ // $@!bbb!n 5$$@$!a@ // $!aaa!$! bbb$urn:foo$ 0x42, 0, 0x0C, 4, 0xA8, 0x0D, 6, 0x98, 1, 0x76, 0x07, 3, 0x61, 0x61, 0x61, 8, 0xA8, // 16 0x05, 3, 0x62, 0x62, 0x62, 2, 0x6E, 0x35, 0xA8, 0x04, 2, 0x6E, 0x36, 0xA8, // 30 0x06, 12, 0xA8, 0x05, 3, 0x62, 0x62, 0x62, 2, 0x6E, 0x38, 0xA8, 0x0A, 2, 0x0B, 1, 0x61, 10, // 48 0x0B, 1, 0x62, 2, 0x0B, 3, 0x61, 0x61, 0x61, 10, 0x09, 3, 0x62, 0x62, 0x62, 0x07, 0x75, 0x72, 0x6E, 0x3A, 0x66, 0x6F, 0x6F, 1 }; Assert.AreEqual(bytes, ms.ToArray(), "result"); }