I have two sample classes that are used for de/serializing an XML file in tests.
XmlExampleBasicUnit.cs:
[YAXSerializeAs("Unit")]
public class XmlExampleBasicUnit
{
[YAXSerializeAs("StringVar")]
public String StringVar { get; set; }
[YAXSerializeAs("Int32Var")]
public Int32 Int32Var { get; set; }
[YAXSerializeAs("DoubleVar")]
public Double DoubleVar { get; set; }
[YAXSerializeAs("DateTimeVar")]
public String DateTimeVar { get; set; }
[YAXSerializeAs("CharVar")]
public String CharVar { get; set; }
}
XmlExampleCollectionOfUnits.cs:
[YAXSerializeAs("CollectionOfUnits")]
public class XmlExampleCollectionOfUnits
{
[YAXSerializeAs("Units")]
public List<XmlExampleBasicUnit> Units { get; set; }
public XmlExampleCollectionOfUnits(List<XmlExampleBasicUnit> units)
{
Units = units;
}
public XmlExampleCollectionOfUnits()
{
}
}
I want to export a variable of XmlExampleCollectionOfUnits
into an XML file that would be correctly formatted. This is a current function that is responsible for this:
public void Export<T>(String fileName, T result) where T : class
{
YAXSerializer serializer = new YAXSerializer(typeof(T));
using (FileStream fs = File.Create(fileName))
{
Byte[] info = new UTF8Encoding(true).GetBytes(serializer.Serialize(result));
fs.Write(info, 0, info.Length);
}
}
The problem is that this is using GetBytes()
function which add some byte which is unrecognized by function that is responsible for importing data from an XML file. I have been trying to work this out with XmlTextWriter
but with no success. I can get the String
of a the content that I want to write into a file, but XmlTextWriter
can't handle it.