PDFsharp & MigraDoc Foundation
http://forum.pdfsharp.com/

XUNIT on XML XmlSerializer Problems
http://forum.pdfsharp.com/viewtopic.php?f=2&t=4348
Page 1 of 1

Author:  Jupklass [ Thu Jun 23, 2022 10:05 am ]
Post subject:  XUNIT on XML XmlSerializer Problems

Hi.
I need to save several XUnit on a xml file to save the "project".
For xml creation i use XmlSerializer Serialize to save / create the file. That works great

for read the xml file i use Deserialize
the problem is that Deserialize read all the varialbles fine except the XUnit that has very different values of the values on the xml file

Any help? Thanks

public static void SaveXML(string FilePath, Proyect CurrentProyect)
{
System.Xml.Serialization.XmlSerializer ToFile = new System.Xml.Serialization.XmlSerializer(CurrentProyect.GetType());
XmlTextWriter XmlWriter = new XmlTextWriter(new FileStream(FuncionesGenerales.ValidaNombre(FilePath), FileMode.Create), Encoding.Unicode);
XmlWriter writer = XmlWriter;
ToFile .Serialize(writer, CurrentProyect);
XmlWriter.WriteEndElement();
writer.Close();
}

create xml file (for XUnit) that´s correct

<TamanoCarta_X>
<Point>180</Point>
<Inch>2.5</Inch>
<Millimeter>63.5</Millimeter>
<Centimeter>6.35</Centimeter>
<Presentation>240</Presentation>
</TamanoCarta_X>

reading code

XmlSerializer ser = new XmlSerializer(typeof(CurrentProyect));
NewConfig = (ser.Deserialize(stm) as CurrentProyect);

Image

Thanks

Author:  TH-Soft [ Thu Jun 23, 2022 10:50 am ]
Post subject:  Re: XUNIT on XML XmlSerializer Problems

I don't see "Type" in your XML snippet.
All you have to save is Value and Type. Forget about inch, point, cm, etc.

Author:  Jupklass [ Thu Jun 23, 2022 1:28 pm ]
Post subject:  Re: XUNIT on XML XmlSerializer Problems

TH-Soft wrote:
I don't see "Type" in your XML snippet.
All you have to save is Value and Type. Forget about inch, point, cm, etc.


XMLserializer converts automatically all variable to xml file. is something from c#
if i create a xml my own where i write, let´s say xml.write(myxunit.millimeter) and then when read it like myunit=Xunit.frommillimeter works fine

but i have a 100 line code as i have to save each project variable while using serialize i only need 3 lines :(

Author:  Thomas Hoevel [ Thu Jun 23, 2022 2:50 pm ]
Post subject:  Re: XUNIT on XML XmlSerializer Problems

There is a bug in the Presentation property of the XUnit class.
You can quickly fix it when you take the source code from GitHub and compile it after making a simple chance.

Here's the code:
Code:
public double Presentation
{
    get
    {
        switch (_type)
        {
            case XGraphicsUnit.Point:
                return _value * 96 / 72;

            case XGraphicsUnit.Inch:
                return _value * 96;

            case XGraphicsUnit.Millimeter:
                return _value * 96 / 25.4;

            case XGraphicsUnit.Centimeter:
                return _value * 96 / 2.54;

            case XGraphicsUnit.Presentation:
                return _value;

            default:
                throw new InvalidCastException();
        }
    }
    set
    {
        _value = value;
        _type = XGraphicsUnit.Point;
    }
}

Near the bottom, instead of "_type = XGraphicsUnit.Point;" it should be "_type = XGraphicsUnit.Presentation;".

The bug would have no effect if Presentation were not the last property in the XML.

As a workaround, you could delete all properties from the "<TamanoCarta_X>" element except the "<Point>180</Point>" element. This would eliminate potential rounding errors.

Jupklass wrote:
but i have a 100 line code as i have to save each project variable while using serialize i only need 3 lines :(
Three lines only, but risk of rounding errors.

Author:  Jupklass [ Fri Jun 24, 2022 10:06 am ]
Post subject:  Re: XUNIT on XML XmlSerializer Problems

Thomas Hoevel wrote:
There is a bug in the Presentation property of the XUnit class.
You can quickly fix it when you take the source code from GitHub and compile it after making a simple chance.


Thank you so much!

Author:  Jupklass [ Mon Jun 27, 2022 7:38 am ]
Post subject:  Re: XUNIT on XML XmlSerializer Problems

Work done!
It seems to work fine
Thank you so much again.

In case you are curious, my project made thanks to your dll is this https://boardgamegeek.com/thread/249083 ... bletop-pnp

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
https://www.phpbb.com/