PDFsharp & MigraDoc Foundation

PDFsharp - A .NET library for processing PDF & MigraDoc Foundation - Creating documents on the fly
It is currently Tue Mar 19, 2024 6:15 am

All times are UTC


Forum rules


Please read this before posting on this forum: Forum Rules



Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Thu Jun 23, 2022 10:05 am 
Offline

Joined: Thu Jun 23, 2022 9:38 am
Posts: 10
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


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 23, 2022 10:50 am 
Offline
PDFsharp Expert
User avatar

Joined: Sat Mar 14, 2015 10:15 am
Posts: 905
Location: CCAA
I don't see "Type" in your XML snippet.
All you have to save is Value and Type. Forget about inch, point, cm, etc.

_________________
Best regards
Thomas
(Freelance Software Developer with several years of MigraDoc/PDFsharp experience)


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 23, 2022 1:28 pm 
Offline

Joined: Thu Jun 23, 2022 9:38 am
Posts: 10
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 :(


Top
 Profile  
Reply with quote  
PostPosted: Thu Jun 23, 2022 2:50 pm 
Offline
PDFsharp Guru
User avatar

Joined: Mon Oct 16, 2006 8:16 am
Posts: 3092
Location: Cologne, Germany
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.

_________________
Regards
Thomas Hoevel
PDFsharp Team


Top
 Profile  
Reply with quote  
PostPosted: Fri Jun 24, 2022 10:06 am 
Offline

Joined: Thu Jun 23, 2022 9:38 am
Posts: 10
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!


Top
 Profile  
Reply with quote  
PostPosted: Mon Jun 27, 2022 7:38 am 
Offline

Joined: Thu Jun 23, 2022 9:38 am
Posts: 10
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


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 26, 2022 9:48 am 
Offline

Joined: Thu Jun 23, 2022 9:38 am
Posts: 10
I´m migrating the tool form windows forms with .net.4.8 to winui with .net and I have again the same issue.
The dll works weel with the change on .net.4.8 but not on .net 6
I will try some aproach and i will let you know


Top
 Profile  
Reply with quote  
PostPosted: Mon Sep 26, 2022 11:35 am 
Offline

Joined: Thu Jun 23, 2022 9:38 am
Posts: 10
Jupklass wrote:
I´m migrating the tool form windows forms with .net.4.8 to winui with .net and I have again the same issue.
The dll works weel with the change on .net.4.8 but not on .net 6
I will try some aproach and i will let you know


Problem solved. Just post it here just in case.
The project automatically download the nuget package (the one with the bug)
even if i delete it and add the good reference it still read the nuget package

close visual studio. delete folder with nuget pacakge under lib. Open visual studio. reload good dll

Thanks


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC


Who is online

Users browsing this forum: Google [Bot] and 46 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Privacy Policy, Data Protection Declaration, Impressum
Powered by phpBB® Forum Software © phpBB Group