public void DeserializeExceptionTest2() { string soap = @"<?xml version=""1.0"" encoding=""utf-8""?> <S:Envelope xmlns:S=""http://schemas.xmlsoap.org/soap/envelope/""> <S:Body> <S:Fault xmlns:ns4=""http://www.w3.org/2003/05/soap-envelope""> <faultcode>S:Client</faultcode> <faultstring>Cannot find distribution method for {http://tempuri.org/}testException.</faultstring> </S:Fault> </S:Body> </S:Envelope>"; SoapSerializer serializer = new SoapSerializer("http://tempuri.org/"); Assert.ThrowsAsync <ProtocolViolationException>(async() => await serializer.Deserialize <Vector>(soap)); }
public void DeserializeExceptionTest() { string soap = @"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:tns=""http://tempuri.org/"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> <soap:Header /> <soap:Body> <soap:Fault> <faultcode>System.IO.DirectoryNotFoundException</faultcode> <faultstring>The directory was not found.</faultstring> </soap:Fault> </soap:Body> </soap:Envelope>"; SoapSerializer serializer = new SoapSerializer("http://tempuri.org/"); Assert.ThrowsAsync <ProtocolViolationException>(async() => await serializer.Deserialize <Vector>(soap)); }
public async Task DeserializeTest() { string soap = @"<?xml version=""1.0"" encoding=""utf-8""?> <soap:Envelope xmlns:tns=""http://tempuri.org/"" xmlns:soap=""http://schemas.xmlsoap.org/soap/envelope/""> <soap:Header /> <soap:Body> <tns:Point> <tns:X>9.6</tns:X> <tns:Y>4.2</tns:Y> </tns:Point> </soap:Body> </soap:Envelope>"; SoapSerializer serializer = new SoapSerializer("http://tempuri.org/"); Vector v = await serializer.Deserialize <Vector>(soap); TestContext.WriteLine(v); Assert.Pass(); }
static void Main(string[] args) { var diamond = new Diamond { Price = 113, Weight = 45 }; var stone1 = new Stone { Color = "Белый", Price = 20, Status = ProductStatus.Ready, Weight = 100 }; var pStone1 = new PreciousStone { Color = "Желтый", Price = 40, Status = ProductStatus.Ready, Weight = 40 }; var ruby1 = new Ruby { Price = 57, Status = ProductStatus.None, Weight = 30 }; var diamond1 = new Diamond { Price = 89, Status = ProductStatus.Ready, Weight = 28 }; List <Stone> stones = new List <Stone> { stone1, pStone1, ruby1, diamond1, diamond }; var necklace = new Necklace(stones); var binSerializer = new BinarySerializer <Diamond>("doc/diamond.txt"); binSerializer.Serialize(diamond); var bsDiamond = binSerializer.Deserialize(); Console.WriteLine(bsDiamond); var soapSerializer = new SoapSerializer <Diamond>("doc/diamond.soap"); soapSerializer.Serialize(diamond); var ssDiamond = soapSerializer.Deserialize(); Console.WriteLine(ssDiamond); var xmlSerializer = new XmlSerializer <Diamond>("doc/diamond.xml"); xmlSerializer.Serialize(diamond); var xsDiamond = xmlSerializer.Deserialize(); Console.WriteLine(xsDiamond); var jsonSerializer = new JsonSerializer <Diamond>("doc/diamond.json"); jsonSerializer.Serialize(diamond); var jsDiamond = jsonSerializer.Deserialize(); Console.WriteLine($"{jsDiamond} \n"); var xml = new XmlSerializer <Necklace>("doc/necklace.xml"); xml.Serialize(necklace); var xsNecklace = xml.Deserialize(); var soapStones = new SoapSerializer <Stone[]>("doc/stones.soap"); soapStones.Serialize(stones.ToArray()); var ssStones = soapStones.Deserialize(); XmlDocument xDoc = new XmlDocument(); xDoc.Load("doc/necklace.xml"); XmlElement xRoot = xDoc.DocumentElement; //xpath - язык запросов к XML-документам Console.WriteLine("Вывод элементов, у которых вес больше 35"); XmlNodeList childnodes = xRoot.SelectNodes("//Stones/Stone[@Weight>35]"); foreach (XmlNode n in childnodes) { var prodType = n.SelectSingleNode("ProductType").InnerText; var color = n.SelectSingleNode("Color").InnerText; var price = n.SelectSingleNode("Price").InnerText; Console.WriteLine($"{color} {prodType} - ${price}"); } Console.WriteLine("\nВывод элементов всех элементов с типом Алмаз:"); childnodes = xRoot.SelectNodes("//Stones/Stone[ProductType='Алмаз']"); foreach (XmlNode n in childnodes) { var prodType = n.SelectSingleNode("ProductType").InnerText; var weight = n.SelectSingleNode("@Weight").Value; var price = n.SelectSingleNode("Price").InnerText; Console.WriteLine($"{prodType} - ${price}, {weight} грамм"); } Console.WriteLine("Создание документа books.xml:"); XDocument qDoc = new XDocument(); XElement books = new XElement("books"); XElement book1 = CreateBookElement("Neil Gaiman", "American Gods", 23, 432); XElement book2 = CreateBookElement("J. K. Rowling", "Harry Potter", 17, 345); XElement book3 = CreateBookElement("William Shakespeare", "Hamlet", 27, 290); XElement book4 = CreateBookElement("George Martin", "The Song of Ice and Fire", 30, 550); books.Add(book1, book2, book3, book4); qDoc.Add(books); qDoc.Save("doc/books.xml"); Console.WriteLine("\nЭлементы у которых pages больше 350:"); XDocument xdoc = XDocument.Load("doc/books.xml"); var items = xdoc.Element("books").Elements("book").Where(xe => int.Parse(xe.Element("pages").Value) > 350).ToList(); foreach (var item in items) { Console.WriteLine(item.ToString()); } Console.WriteLine("\nКниги с ценой меньше 25:"); var booksQuery = from xe in xdoc.Element("books").Elements("book") where int.Parse(xe.Attribute("price").Value) < 25 select new { Name = xe.Element("name").Value, Author = xe.Element("author").Value }; foreach (var book in booksQuery) { Console.WriteLine($"{book.Author} - {book.Name}"); } }
private void CompareDeserializes <T>(T obj, int runs) { var objType = obj.GetType(); //warm-up byte[] protobufData, binaryData, dataContractData, soapData, netData; #if NETFULL || NETCOREAPP3_1 var jilSerializedText = jilserializer.SerializeToString(obj); using (MemoryStream mem = new MemoryStream()) { binaryserializer.Serialize(obj, mem); binaryData = mem.ToArray(); } using (MemoryStream mem = new MemoryStream()) { datacontractserializer.Serialize(obj, mem); dataContractData = mem.ToArray(); } #endif #if NETFULL using (MemoryStream mem = new MemoryStream()) { soapserializer.Serialize(obj, mem); soapData = mem.ToArray(); } #endif var netserializer = SerializerFactory.Get("NET"); using (MemoryStream mem = new MemoryStream()) { netserializer.Serialize(obj, mem); netData = mem.ToArray(); } var jsonnetSerializedText = jsonnetserializer.SerializeToString(obj); var textjsonSerializedText = textjsonserializer.SerializeToString(obj); var xmlSerializedText = xmlserializer.SerializeToString(obj); using (MemoryStream mem = new MemoryStream()) { protobufserializer.Serialize(obj, mem); protobufData = mem.ToArray(); } var keys = serializer.Keys.ToList(); #if NETFULL || NETCOREAPP3_1 serializer["Jil"].Act = () => { GC.Collect(2, GCCollectionMode.Forced, blocking: true); serializer["Jil"].Score = Helper.AverageRuntime(() => { jilserializer.DeserializeFromString(jilSerializedText, objType); }, runs); }; serializer["Binary"].Act = () => { GC.Collect(2, GCCollectionMode.Forced, blocking: true); serializer["Binary"].Score = Helper.AverageRuntime(() => { using (MemoryStream mem = new MemoryStream(binaryData)) { binaryserializer.Deserialize(mem, objType); } }, runs); }; serializer["DataContract"].Act = () => { GC.Collect(2, GCCollectionMode.Forced, blocking: true); serializer["DataContract"].Score = Helper.AverageRuntime(() => { using (MemoryStream mem = new MemoryStream(dataContractData)) { datacontractserializer.Deserialize(mem, objType); } }, runs); }; #endif #if NETFULL serializer["Soap"].Act = () => { GC.Collect(2, GCCollectionMode.Forced, blocking: true); serializer["Soap"].Score = Helper.AverageRuntime(() => { using (MemoryStream mem = new MemoryStream(soapData)) { soapserializer.Deserialize(mem, objType); } }, runs); }; #endif serializer["NET"].Act = () => { GC.Collect(2, GCCollectionMode.Forced, blocking: true); serializer["NET"].Score = Helper.AverageRuntime(() => { using (MemoryStream mem = new MemoryStream(netData)) { netserializer.Deserialize(mem, objType); } }, runs); }; serializer["Json"].Act = () => { GC.Collect(2, GCCollectionMode.Forced, blocking: true); serializer["Json"].Score = Helper.AverageRuntime(() => { jsonnetserializer.DeserializeFromString(jsonnetSerializedText, objType); }, runs); }; serializer["TextJson"].Act = () => { GC.Collect(2, GCCollectionMode.Forced, blocking: true); serializer["TextJson"].Score = Helper.AverageRuntime(() => { textjsonserializer.DeserializeFromString(textjsonSerializedText, objType); }, runs); }; serializer["Xml"].Act = () => { GC.Collect(2, GCCollectionMode.Forced, blocking: true); serializer["Xml"].Score = Helper.AverageRuntime(() => { xmlserializer.DeserializeFromString(xmlSerializedText, objType); }, runs); }; serializer["Protobuf"].Act = () => { GC.Collect(2, GCCollectionMode.Forced, blocking: true); serializer["Protobuf"].Score = Helper.AverageRuntime(() => { using (MemoryStream mem = new MemoryStream(protobufData)) { protobufserializer.Deserialize(mem, objType); } }, runs); }; keys.ForEach(k => { serializer[k].Act(); }); }