I´m a programming student and I would like to know if it is possible to change the format of a date when I serialize it in a xml file. This date is an attribute of an ObservableCollection of objects "Loan", this objects have two DateTime properties, one of the dates is a nullable object. I serialize all the collection including the date.
I would like to obtain in the xml file:
<OutDate> 15-03-2014 </OutDate>
<!--If the date is null I don´t want to appear the node-->
And I´m getting this:
<OutDate>2014-03-15T00:00:00</OutDate>
<InDate xsi:nil="true" />
This is part of my code project:
Part of my class Loan, already mark as serializable, looks like this:
private string isbn;
private string dni;
private DateTime dateOut;
private DateTime? dateIn;
// Setters and Gettters and constructors
This is the method for serialize:
// I will pass three collections to this method loans, books and clients
public void SerializeToXML<T>(string file, string node, ObservableCollection<T> collection)
{
XmlRootAttribute root = new XmlRootAttribute(node);
XmlSerializer serializer = new XmlSerializer(typeof(ObservableCollection<T>), root);
using (FileStream fs = new FileStream(file, FileMode.Create))
{
serializer.Serialize(fs, collection);
}
}
The call:
SerializeToXML<Loan>(_file, "Library", manager.LoansCollection);
Thnks.
If you don't want to implement IXmlSerializable, some DateTime to string conversion of a backing field should do the trick, something like this:
public class Loan
{
[XmlIgnore]
private DateTime _dateOut;
public string OutDate
{
get { return _dateOut.ToString("dd-MM-yyyy"); }
set { _dateOut = DateTime.Parse(value); }
}
}
Probably the simplest way to achieve this is to implement the IXmlSerializable interface on your class instead. Something along the following lines
public class Loan : IXmlSerializable
{
public void WriteXml(XmlWriter writer)
{
if(dateIn.HasValue)
{
writer.WriteElementString("dateIn", dateIn.Value.ToString());
}
}
}
On the read you'll need to read the Element name, if it's dateIn set that, otherwise set the appropriate value. Basically checking to see if it exists in the XML.
Have a look at the XmlElement attribute class (in System.Xml.Serialization). If that doesn't work then this answer shows how to use a proxy property
[XmlElement("TheDate", DataType = "date")]
public DateTime TheDate { get; set; }
I know it's late to get my answer marked as "the one", but you can have control over serialization without implementing complex interfaces or wrapping stuff as a workaround.
public DateTime? InDate { get; set }
public bool ShouldSerializeInDate()
{
return InDate.HasValue;
}
The C# XML Serializer has a not so well documented functionality. Every public property can have a method for turning on or off the serialization of the property. The method has to be called: ShouldSerializeXYZ where XYZ is the exact name of the property that you want to control.
See:
Xml serialization - Hide null values
Related
Here's a self-contained sample program that demonstrates my question: https://gist.github.com/jennings/46c20733df559d02b9ad
I'm writing a type Maybe<T> which looks like this:
public struct Maybe<T> : IXmlSerializable where T : class
{
readonly T _value;
//...
}
Can I accomplish one of these two goals of the serialization?
If a property of type Maybe<T> is being serialized, do not emit the property at all if its _value is null (string properties seem to have this behavior).
Serialize the _value in place of the Maybe<T>.
So if a class using my type looks like this:
public class AnyRandomObjectUsingMyType
{
public Maybe<string> MaybeAString{ get; set; }
}
Then if MaybeaString._value is null, I want this when serialized:
<AnyRandomObjectUsingMyType>
</AnyRandomObjectUsingMyType>
I do not want either of these:
<AnyRandomObjectUsingMyType>
<MaybeAString xsi:nil="true" />
</AnyRandomObjectUsingMyType>
<AnyRandomObjectUsingMyType>
<MaybeAString></MaybeAString>
</AnyRandomObjectUsingMyType>
I don't want to configure the serialization on AnyRandomObjectUsingMyType because there will be many, many of those. I'd much rather have Maybe<T> control this so everywhere gets the same behavior.
Some context
The type Maybe<T> is meant as a drop-in replacement for a T. But we use XML serialization in several places. If there's an object like this:
// Version 1 class
public class SomethingBeingSerialized
{
public string SomeValue { get; set; }
}
When SomeValue is null, this class serializes to this:
<SomethingBeingSerialized>
</SomethingBeingSerialized>
I want to be able to change the class to this:
// Version 2 class
public class SomethingBeingSerialized
{
public Maybe<string> SomeValue { get; set; }
}
And I want the serialized representation to be identical. If a V1 app deserializes, it gets a null string. If a V2 app deserializes, it gets a Maybe<string> which represents None.
I've tried doing this in my IXmlSerializable.WriteXml method, but when it gets called, the XmlWriter seems to already have written this much to it's underlying stream:
<SomeValue
So it seems like by the time my WriteXml method gets called, it's too late. Maybe there's another way to control this?
If I could specify to the serializer: "As soon as you encounter an object of type Maybe<T>, serialize its _value property in place of the Maybe<T>", it would also accomplish my goal.
Do not make Maybe serializable, make it's container serializable. You can have an extension method for Maybe that builds an XML Node for it's value as you desire, and in the class that's being serialized would incorporate the return value of the extension method.
public static class MaybeMethods
{
public static string SerializeValue<T>(this Maybe<T> source)
{
// Do the work to serialize the value in Maybe
}
}
In your hosting classes's serialization method
public void WriteXml(XmlWriter writer)
{
var maybeXml = myMaybe.SerializeValue();
// if maybeXml is not null, include it in your writer
}
I am currently using a LINQ query to read an XML file e.g.
<MyObjects>
<MyObject>
<MyElement>some_text</MyElement>
<MyOtherElement>some_more_text</MyOtherElement>
</MyObject>
</MyObjects>
into a list of custom objects containing custom HistoryString properties. HistoryString contains 2 strings, a currentValue and a previousValue.
This all works great except when using XmlSerializer to write the custom objects back to an XML file, the output fairly obviously contains additional tags i.e.
<MyObjects>
<MyObject>
<MyElement>
<currentValue>some_text</currentValue>
<previousValue>some_text</previousValue>
</MyElement>
<MyOtherElement>
<currentValue>some_more_text</currentValue>
<previousValue>some_more_text</previousValue>
</MyOtherElement>
</MyObject>
</MyObjects>
Q: What would be the neatest and/or most efficient way of reading and writing XML in the same format, based on this fundamental difference?
Some initial ideas:
1) Mark the previousValue property with [System.Xml.Serialization.XmlIgnore] then sweep through the XML string that is to be written removing all traces of <currentValue> and </currentValue>
2) Open the existing file and manually make any updates/deletes/additions - this is surely more long winded.
3) Any way of having a HistoryString automatically resolve to its currentValue rather than serialize each of its properties, similar to how ToString() works?
I have done some research into this, including the useful MSDN articles here and here but I can't see any other attributes that would solve this problem, I am still unsure whether this is possible. Any ideas?
Here is another idea. If you define your class like so:
[Serializable]
public class MyObject
{
[XmlElement(ElementName = "MyElement")]
public string CurrentValueElement
{
get
{
return Element.CurrentValue;
}
set
{
Element = new MyElement
{
CurrentValue = value, PreviousValue = value
};
}
}
[XmlElement(ElementName = "MyOtherElement")]
public string CurrentValueOtherElement
{
get
{
return OtherElement.CurrentValue;
}
set {}
}
[XmlIgnore]
public MyElement Element { get; set; }
[XmlIgnore]
public MyElement OtherElement { get; set; }
}
Then, when the object is serialized, the output XML will look exactly like your example.
Also, if you extend the CurrentValueElement/CurrentValueOtherElement setter like this:
[XmlElement(ElementName = "MyElement")]
public string CurrentValueElement
{
get
{
return Element.CurrentValue;
}
set
{
Element = new MyElement
{
CurrentValue = value, PreviousValue = value
};
}
}
Then you'll be able to use the XmlSerializer to deserialize your objects directly without needing to resorting to LINQ.
Well why not serialize back using original schema and feeding into it the list of transformed objects from history using only current value?
e.g.
from h in HistoryEntryList
select new OriginalEntry{ field = h.field.current_value, ... };
i'm serialization a class but i can't exclude some field in my class.
[Serializable]
public class DicData
{
private GDicJson DeserializedGDicJson = new GDicJson();
public UOCDicData BuiltDicData;
[NonSerialized]
public string CacheName = "";
}
in my expection, a public field CacheName didn't include in my *.xml deserialized output, but it included in .xml file.
here are serializing rutine.
XmlSerializer myXml = new XmlSerializer(typeof(DicData), "test");
myXml.Serialize(myFile, this); //note:a serializing perform in method of himself.
For XmlSerializer you want
[XmlIgnore]
Also, note that the [Serializable] is unnecessary in this case.
As a final note: public fields are not encouraged; properties are almos always preferred. The addition of {get;set;} would go a long way...
I'm trying to serialize a class that derives from BindingList(Floor), where Floor is a simple class that only contains a property Floor.Height
Here's a simplified version of my class
[Serializable]
[XmlRoot(ElementName = "CustomBindingList")]
public class CustomBindingList:BindingList<Floor>
{
[XmlAttribute("publicField")]
public string publicField;
private string privateField;
[XmlAttribute("PublicProperty")]
public string PublicProperty
{
get { return privateField; }
set { privateField = value; }
}
}
I'll serialize an instance of CustomBindingList using the following code.
XmlSerializer ser = new XmlSerializer(typeof(CustomBindingList));
StringWriter sw = new StringWriter();
CustomBindingList cLIst = new CustomBindingList();
Floor fl;
fl = new Floor();
fl.Height = 10;
cLIst.Add(fl);
fl = new Floor();
fl.Height = 10;
cLIst.Add(fl);
fl = new Floor();
fl.Height = 10;
cLIst.Add(fl);
ser.Serialize(sw, cLIst);
string testString = sw.ToString();
Yet testString above ends getting set to the following XML:
<CustomBindingList xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
<Floor Height="10" />
<Floor Height="10" />
<Floor Height="10" />
</CustomBindingList>"
How do I get "publicField" or "publicProperty to serialize as well?
The short answer here is that .NET generally expects something to be a collection xor to have properties. This manifests in a couple of places:
xml serialization; properties of collections aren't serialized
data-binding; you can't data-bind to properties on collections, as it implicitly takes you to the first item instead
In the case of xml serialization, it makes sense if you consider that it might just be a SomeType[] at the client... where would the extra data go?
The common solution is to encapsulate a collection - i.e. rather than
public class MyType : List<MyItemType> // or BindingList<...>
{
public string Name {get;set;}
}
public class MyType
{
public string Name {get;set;}
public List<MyItemType> Items {get;set;} // or BindingList<...>
}
Normally I wouldn't have a set on a collection property, but XmlSerializer demands it...
XML serialization handles collections in a specific way, and never serializes the fields or properties of the collection, only the items.
You could either :
implement IXmlSerializable to generate and parse the XML yourself (but it's a lot of work)
wrap your BindingList in another class, in which you declare your custom fields (as suggested by speps)
This is known issue with XML serialization and inheriting from collections.
You can read more info on this here : http://social.msdn.microsoft.com/Forums/en-US/asmxandxml/thread/0d94c4f8-767a-4d0f-8c95-f4797cd0ab8e
You could try something like this :
[Serializable]
[XmlRoot]
public class CustomBindingList
{
[XmlAttribute]
public string publicField;
private string privateField;
[XmlAttribute]
public string PublicProperty
{
get { return privateField; }
set { privateField = value; }
}
[XmlElement]
public BindingList<Floor> Floors = new BindingList<Floor>();
}
This means you can add floors by using Floors.Add and you will get the result you want, I hope, however, I didn't try it. Keep in mind that playing around with attributes is the key to XML serialization.
Could you guys help me I have a problem with deserialization via IXmlSerializable
var ArrayOfAccounts = new Accounts(); //This class structure I'm trying to read
Class Accounts:List<Session>{ }
Class Shedule{
public DateTime StartAt { get; set; }
public DateTime EndAt { get; set; }
}
Class Session:IXmlSerializable {
public string Name{get;set;}
public string Pass{get;set;}
public List<Shedule> Shedules = new List<Shedule>();
public void ReadXml(System.Xml.XmlReader reader){
//AND HERE IS A PROBLEM. I don't know how to implement right code here. I've tried
//code below, but this one works for the first account only, and doesn't restore others
Schedules.Clear();
XmlReader subR = reader.ReadSubtree();
if (reader.MoveToAttribute("Name"))
Name = reader.Value;
if (reader.MoveToAttribute("Password"))
Password = reader.Value;
reader.MoveToContent();
while (subR.ReadToFollowing("Schedule"))
{
XmlSerializer x = new XmlSerializer(typeof(Schedule));
object o = x.Deserialize(subR);
if (o is Schedule) Schedules.Add((Schedule)o);
}
}
And the xml itself looks like:
<Accounts>
<Session UserName="18SRO" Password="shalom99">
<Schedule>
<StartAt>0001-01-01T09:30:00</StartAt>
<EndAt>0001-01-01T16:00:00</EndAt>
</Schedule>
</Session>
</Accounts>
Since you've defined the classes, you should just be able to use XML Serialization attributes, and use the default XML deserializer.
Your structure doesn't look overly complicated, is there any particular reason you're not using serialization attributes instead of manually deserializing?
Re inherited fields... if you switch to DataContractSerializer, then fields are "opt in" rather than "opt out" - but you lose the ability to specify attributes (everything is an element). A trivial example:
[DataContract(Name="foo")]
public class Foo
{
[DataMember(Name="bar")]
public string Bar { get; set; }
public int ThisIsntSerialized {get;set;}
}
However - adding unexpected subclasses is a pain for both XmlSerializer and DataContractSerializer. Both can do it, but it isn't pretty...