Dictionary serialization with DataContractSerializer generate below data. How to replace d2p1:KeyValueOfintint
, d2p1:Key
and d2p1:Value
with our own attributes / tags / identifier.
Serializing Dictionary in [CashCounter],
Output Generate after Serialization given below
<CashCounter xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/DictionarySerlization">
<BankNote xmlns:d2p1="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
<d2p1:KeyValueOfintint>
<d2p1:Key>10</d2p1:Key>
<d2p1:Value>6</d2p1:Value>
</d2p1:KeyValueOfintint>
<d2p1:KeyValueOfintint>
<d2p1:Key>5</d2p1:Key>
<d2p1:Value>10</d2p1:Value>
</d2p1:KeyValueOfintint>
</BankNote>
<TotalCount>16</TotalCount>
<TotalSum>110</TotalSum>
</CashCounter>
static void Main(string[] args) {
CashCounter cashCounter = addCashCounter();
string serilizedData = GetXml(cashCounter);
}
private static CashCounter addCashCounter() {
CashCounter cashCounter = CreateCounter();
for(var i = 0; i < 6; i++) { cashCounter = incrementCountAmount(cashCounter, 10); }
for(var i = 0; i < 10; i++) { cashCounter = incrementCountAmount(cashCounter, 5); }
return cashCounter;
}
private static CashCounter CreateCounter()
{
var cashCounter = new CashCounter
{
BanknotesCount = new Dictionary<int, int>(),
TotalSum = 0,
TotalCount = 0
};
return cashCounter;
}
private static CashCounter incrementCountAmount(CashCounter cashCounter, int amount){
const int count = 1;
cashCounter.TotalCount += count;
cashCounter.TotalSum += amount * count;
if (cashCounter.BanknotesCount.ContainsKey(amount))
{
cashCounter.BanknotesCount[amount] += count;
}
else
{
cashCounter.BanknotesCount.Add(amount, count);
}
return cashCounter;
}
public static string GetXml<T>(T obj, DataContractSerializer serializer)
{
using (var textWriter = new StringWriter())
{
var settings = new XmlWriterSettings {OmitXmlDeclaration = true,Indent = true, IndentChars = " " };
using (var xmlWriter = XmlWriter.Create(textWriter, settings))
{
serializer.WriteObject(xmlWriter, obj);
}
return textWriter.ToString();
}
}
public static string GetXml<T>(T obj)
{
var serializer = new DataContractSerializer(typeof(T));
return GetXml(obj, serializer);
}