Autor Beitrag
MoBBer
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37
Erhaltene Danke: 1



BeitragVerfasst: Mi 08.07.09 13:37 
Hallo zusammen,

ich hab mal wieder ne Fragen. Kann ich ein mit [XmlElement] deklariertes Objekt, beim Serialisieren ignorieren wenn der Wert des Objektes 0 ist??

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
        
        [XmlAttribute("Name")]
        public string name;
        [XmlAttribute("Preis")]
        public double preis;
        [XmlElement("Rabatt")]
        public int rabatt;

        public Item()
        {
        }

        public Item(string name, double preis)
        {
            this.name = name;
            this.preis = preis;
        }

        public Item(string name, double preis, int prozent)
        {
            this.name = name;
            this.preis = preis;
            this.rabatt = prozent;
        }


und zwar soll der Rabatt halt nur in der XML angezeigt werden wenn es auch wirklich Rabatt gibt.

Danke schonmal im Vorraus.


Moderiert von user profile iconKha: Topic aus WinForms verschoben am Mi 08.07.2009 um 16:17
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mi 08.07.09 14:22 
XmlSerializer Class hat folgendes geschrieben:
If a schema includes an element that is optional (minOccurs = '0'), or if the schema includes a default value, you have two options. One option is to use System.ComponentModel..::.DefaultValueAttribute to specify the default value, as shown in the following code [...]
;)

_________________
>λ=
MoBBer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37
Erhaltene Danke: 1



BeitragVerfasst: Mi 08.07.09 14:43 
Ah die zweite Option scheint perfekt zu passen ich probiers gleich mal aus.
Wer MSDN Online lesen kann ist klar im Vorteil.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
 public class Item
    {
        [XmlAttribute("Name")]
        public string name;
        [XmlAttribute("Preis")]
        public double preis;
        [XmlElement("Rabatt")]
        public int rabatt;
        [XmlIgnoreAttribute]
        public bool rabattSpecified;

        public Item()
        {
        }

        public Item(string name, double preis)
        {
            this.name = name;
            this.preis = preis;
        }

        public Item(string name, double preis, int prozent)
        {
            this.name = name;
            this.preis = preis;
            this.rabatt = prozent;
            this.rabattSpecified = true;
        }
    }

EDIT: funktioniert einwandfrei
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Mi 08.07.09 16:16 
Und warum passt die erste nicht :?!?: ?

ausblenden C#-Quelltext
1:
2:
[DefaultValue(0)]
public int Rabatt;

_________________
>λ=
MoBBer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37
Erhaltene Danke: 1



BeitragVerfasst: Do 16.07.09 08:01 
Ich wollte das die Zeile Rabatt nur in der XML drin steht wenn auch Rabatt gesetzt ist also höher als 0.
Da ich später eine Art Produktübersicht machen soll in der man mit Checkboxes diese Option Rabatt an und ausschalten kann.
Zudem war die Vorgabe so, dass das dann nicht dadrin stehen soll.
Kha
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 3803
Erhaltene Danke: 176

Arch Linux
Python, C, C++ (vim)
BeitragVerfasst: Do 16.07.09 09:49 
Ich bin mir ziemlich sicher, dass es bei meinem Test mit DefaultValueAttribute ebenfalls nicht in die Datei geschrieben wurde...

_________________
>λ=
MoBBer Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 37
Erhaltene Danke: 1



BeitragVerfasst: Do 16.07.09 10:46 
Probier ich gerne aus mom.

Stimmt das klappt auch. Hatte nur den zweiten Weg probiert, weil der erste mir danach aussah, als wenn dann immer der defaultValue reingeschrieben wird.