xmlserializer

How to serialize an ICollection<T> that also has read/write properties to XML

元气小坏坏 提交于 2019-11-26 22:28:32
问题 I have class that implement list of custom class. That class also has two properties. But when I serialize that class, XML contains only array of my custom classes but don't contains another two properties. Here is the class: public class Porudzbina : List<PorudzbenicaStavka>, IEnumerable<SqlDataRecord> { public long KomSifra { get; set; } public Guid KomId { get; set; } IEnumerator<SqlDataRecord> IEnumerable<SqlDataRecord>.GetEnumerator() { var sqlRow = new SqlDataRecord( new SqlMetaData("rb

XmlSerializer ignores [XmlAttribute] in WebApi

白昼怎懂夜的黑 提交于 2019-11-26 21:54:32
问题 I have a WebApi that returns a simple object, but when I'm forcing it to return as XML ( Accept: application/xml ) it ignores the [XmlAttribute] attribute I've set on the object. This is my object: public class Foo { [XmlAttribute] public string Bar { get; set; } } And I return it like this in the code: [RoutePrefix("api/mytest")] public class MyTestController : System.Web.Http.ApiController { [HttpGet] [Route("gettest")] public Foo GetTest() { return new Foo() { Bar = "foobar" }; } } The

Controlling order of serialization in C#

倾然丶 夕夏残阳落幕 提交于 2019-11-26 21:13:34
问题 I'm using an XmlSerializer to serialize an object and write it to a file. I've had quite a bit of success with the serializer doing what I want it to do in terms of nesting elements and what is serialized as elements vs attributes. Unfortunately, I've run into a problem where I need one member of a class to serialize before another. Elsewhere it's worked for me that whatever is declared first gets serialized first, but in this instance I'm not having so much success with that. Is there any

Why doesn't XmlSerializer support Dictionary?

混江龙づ霸主 提交于 2019-11-26 17:52:20
Just curious as to why Dictionary is not supported by XmlSerializer ? You can get around it easily enough by using DataContractSerializer and writing the object to a XmlTextWriter , but what are the characteristics of a Dictionary that makes it difficult for a XmlSerializer to deal with considering it's really an array of KeyValuePairs. In fact, you can pass an IDictionary<TKey, TItem> to a method expecting an IEnumerable<KeyValuePairs<TKey, ITem>> . Hashtables need hashcode and equality comparer providers generally. These cant be serialized easily in XML, and definitely will not be portable.

WCF Error “Maximum number of items that can be serialized or deserialized in an object graph is '65536'”

本小妞迷上赌 提交于 2019-11-26 16:45:07
I am receiving the following error on a WCF call: Maximum number of items that can be serialized or deserialized in an object graph is '65536' I've read a ton of forum posts and many of them mention modifying the app.config and web.config to specify new behavior to allow larger object graphs. I've done that and this is what I have in those files: App.Config on the WPF project: <system.serviceModel> <behaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="false" /> <serviceDebug includeExceptionDetailInFaults="false" /> </behavior> </serviceBehaviors>

Use the XmlInclude or SoapInclude attribute to specify types that are not known statically

余生颓废 提交于 2019-11-26 16:02:47
问题 I've got a very strange problem when working with .NET's XmlSerializer . Take the following example classes: public class Order { public PaymentCollection Payments { get; set; } //everything else is serializable (including other collections of non-abstract types) } public class PaymentCollection : Collection<Payment> { } public abstract class Payment { //abstract methods } public class BankPayment : Payment { //method implementations } AFAIK, there are three different methods to solve the

Deserialize XML To Object using Dynamic

回眸只為那壹抹淺笑 提交于 2019-11-26 12:07:27
Is it possible Deserialize unknown XML to object like below? var xml = @"<Students><Student><Name>Arul</Name><Mark>90</Mark></Student></Students>"; var serializer = new XmlSerializer(typeof(DynamicObject)); dynamic students = serializer.Deserialize(new XmlTextReader(new StringReader(xml))); You may want to try this. string xml = @"<Students> <Student ID=""100""> <Name>Arul</Name> <Mark>90</Mark> </Student> <Student> <Name>Arul2</Name> <Mark>80</Mark> </Student> </Students>"; dynamic students = DynamicXml.Parse(xml); var id = students.Student[0].ID; var name1 = students.Student[1].Name; foreach

FileMode.Open and FileMode.OpenOrCreate difference when file exists? c# bug?

筅森魡賤 提交于 2019-11-26 11:36:47
问题 I have wrote that code: public void Save() { using (FileStream fs = new FileStream(Properties.Settings.Default.settings_file_path, FileMode.Open)) { XmlSerializer ser = new XmlSerializer(typeof(MySettings)); ser.Serialize(fs, this); } } When I am using FileMode.Open everything is good, and output is e.x. like this: <?xml version=\"1.0\"?> <MySettings xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"> <settingsList> <Setting> <Value>12<

ShouldSerialize*() vs *Specified Conditional Serialization Pattern

淺唱寂寞╮ 提交于 2019-11-26 11:24:37
I am aware of both of the ShouldSerialize* pattern and the *Specified pattern and how they work, but is there any difference between the two? Are there any "gotchas" using one method vs the other when certain things should be serialized conditionally? This question is specific to the usage of XmlSerializer , but general information regarding this topic is welcome as well. There is very little information on this topic out there, so it may be because they perform the exact same purpose and it's a style choice. However, it seems strange that the .NET implementers would analyze the class via

XmlSerializer serialize generic List of interface

会有一股神秘感。 提交于 2019-11-26 08:25:14
问题 I\'m trying to use the XmlSerializer to persist a List(T) where T is an interface. The serializer does not like interfaces. I\'m curious if there is a simple way to serialize a list of heterogeneous objects easily with XmlSerializer. Here\'s what I\'m going for: public interface IAnimal { int Age(); } public class Dog : IAnimal { public int Age() { return 1; } } public class Cat : IAnimal { public int Age() { return 1; } } private void button1_Click(object sender, RoutedEventArgs e) { var