xmlserializer

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

萝らか妹 提交于 2019-11-28 02:23:38
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", SqlDbType.Int), new SqlMetaData("RobaSifra", SqlDbType.NVarChar, 50), new SqlMetaData("RobaNaziv",

XmlSerializer ignores [XmlAttribute] in WebApi

China☆狼群 提交于 2019-11-28 01:24:41
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 resulting XML is: <Foo> <Bar>foobar</Bar> </Foo> Whereas I would expect it to be returned like this: <Foo

Rename class when serializing to XML

 ̄綄美尐妖づ 提交于 2019-11-28 01:19:35
I'm trying to serialize the Outer class shown below, and create an XElement from the serialized XML. It has a property which is of type Inner . I'd like to change the name of both Inner (to Inner_X ) and Outer (to Outer_X ). class Program { static void Main(string[] args) { using (MemoryStream memoryStream = new MemoryStream()) { using (TextWriter streamWriter = new StreamWriter(memoryStream)) { var xmlSerializer = new XmlSerializer(typeof(Outer)); xmlSerializer.Serialize(streamWriter, new Outer()); XElement result = XElement.Parse(Encoding.ASCII.GetString(memoryStream.ToArray())); } } } }

Deserializing List<int> with XmlSerializer Causing Extra Items

六眼飞鱼酱① 提交于 2019-11-28 00:50:37
I'm noticing an odd behavior with the XmlSerializer and generic lists (specifically List<int> ). I was wondering if anyone has seen this before or knows what's going on. It appears as though the serialization works fine but the deserialization wants to add extra items to the list. The code below demonstrates the problem. Serializable class: public class ListTest { public int[] Array { get; set; } public List<int> List { get; set; } public ListTest() { Array = new[] {1, 2, 3, 4}; List = new List<int>(Array); } } Test code: ListTest listTest = new ListTest(); Debug.WriteLine("Initial Array: {0}"

XML deserializing only works with namespace in xml

拜拜、爱过 提交于 2019-11-28 00:24:45
问题 The most simple way I get ServiceStack xml deserialization to work is when the xml contains a namespace. However, the xml I receive do not contain namespaces. The most simple working example: [Serializable] public class test { } class Program { static void Main(string[] args) { string xml="<test xmlns=\"http://schemas.datacontract.org/2004/07/\"></test>"; var result = ServiceStack.Text.XmlSerializer.DeserializeFromString<test>(xml); } } However, that is not what I want. I want the following

Controlling order of serialization in C#

女生的网名这么多〃 提交于 2019-11-27 22:53:53
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 way to manually control the order in which things are serialized? Robert Harvey [XmlElementAttribute

How do I add a default namespace with no prefix using XMLSerializer

浪尽此生 提交于 2019-11-27 20:36:55
I am trying to generate an XML document that contains the default namespace without a prefix using XmlSerializer , e.g. <?xml version="1.0" encoding="utf-8" ?> <MyRecord ID="9266" xmlns="http://www.website.com/MyRecord"> <List> <SpecificItem> Using the following code ... string xmlizedString = null; MemoryStream memoryStream = new MemoryStream(); XmlSerializer xs = new XmlSerializer(typeof(ExportMyRecord)); XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces(); xmlnsEmpty.Add(string.Empty, string.Empty); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

XmlSerializer List Item Element Name

天大地大妈咪最大 提交于 2019-11-27 20:24:21
I have a class PersonList [XmlRoot("Persons")] PersonList : List<Human> when I serialize this to XML, by default it will produce something like this: <Persons> <Human>...</Human> <Human>...</Human> </Persons> My question is what needs to be done in order to change element Human to Person in the output? so the output would be : <Persons> <Person>...</Person> <Person>...</Person> </Persons> and, how to deserialize the above XML to the PersonList class object? Per Nick's advice, Here is my testing code: [XmlRoot("Persons")] public class Persons : List<Human> { } [XmlRoot("Person")] public class

Force XML serialization of XmlDefaultValue values

蹲街弑〆低调 提交于 2019-11-27 19:35:59
问题 Using a C# class generated from an XSD document, I can create an object, and serialize it successfully. However, some attributes have an XmlDefaultValue defined. If any objects have the default value, then those attributes do not get created when the object is serialized. This is expected behavior according to the documentation. But this is not how I want it to behave. I need to have all such attributes generated in the XML document. I've checked for any code attributes that can be applied

Is there a way to avoid the XmlSerializer to not initialize a null property when deserializing?

半腔热情 提交于 2019-11-27 17:59:16
问题 I have this class: public class MySerializableClass { public List<MyObject> MyList { get; set; } } If MyList is null when MySerializableClass is serialized, I need to have it null when it's deserialised too, but the XmlSerializer always initializes it when it deserialises my class. Is there a way to avoid it initializing null properties? MyList is not even recorded in the serialized file when it's null. When I load it with null values, and save it again, MyList is not null anymore, it's