0

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.

Pozzi Userpic
  • 347
  • 8
  • 30
  • 1
    I'm confused with this code. What does the YAXSerializer return using the Serialize method()? The XML serializer returns void. The Xml serializer take two parameters the input class and the output stream. I would think you should pass to the Serializer the FileStream fs. I do not like the using statement because it doesn't always report exceptions. I prefer to use try/catch to report exceptions and then make sure after the Write() method use a Flush() and Close(). – jdweng Nov 29 '18 at 12:07
  • Summary: Serializes the specified System.Object and writes the XML document to a file using the specified System.IO.Stream. Parameters: // stream: // The System.IO.Stream used to write the XML document. // o: // The System.Object to serialize. // Exceptions: // System.InvalidOperationException: // An error occurred during serialization. The original exception is available // using the System.Exception.InnerException property. public void Serialize(Stream stream, object o); – jdweng Nov 29 '18 at 12:45
  • Thanks! I have rewrited the code with your suggestions and now it works as I wanted. – Pozzi Userpic Nov 29 '18 at 13:04

1 Answers1

0

The function Serialize of YAXSerializer has an overload for XmlWriter.

Pozzi Userpic
  • 347
  • 8
  • 30