Xml Serialisation von mehreren Objekten
-
[STAThread] static void Main(string[] args) { // // TODO: Fügen Sie hier Code hinzu, um die Anwendung zu starten // Auto auto1 = new Auto("Ford", "Mustang"); Auto auto2 = new Auto("IFA", "Trabi"); Autohaus autohaus = new Autohaus(); try { autohaus.Angebot.Add(auto1); autohaus.Angebot.Add(auto2); XmlSerializer xmls = new XmlSerializer(typeof(Autohaus)); XmlTextWriter xmlw = new XmlTextWriter("C:\\autos.xml",System.Text.Encoding.ASCII); xmls.Serialize(xmlw,autohaus); xmlw.Close(); } catch(System.Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); } } public class Auto { public Auto(string Marke, string Autoname) { this.Marke = Marke; this.Autoname = Autoname; } public Auto() { Marke = ""; Autoname = ""; } private string autoname; private string marke; [XmlElementAttribute(IsNullable = false)] public string Marke { get { return marke; } set { this.marke = value; } } public string Autoname { get { return autoname; } set { this.autoname = value; } } } public class Autos : CollectionBase { public Auto this[ int index ] { get { return( (Auto) List[index] ); } set { List[index] = value; } } public int Add( Auto value ) { return( List.Add( value ) ); } public int IndexOf( Auto value ) { return(List.IndexOf( value ) ); } public void Insert( int index, Auto value ) { List.Insert( index, value ); } public void Remove( Auto value ) { List.Remove( value ); } public bool Contains( Auto value ) { return( List.Contains( value ) ); } protected override void OnInsert( int index, Object value ) { if ( (value is Auto) != true ) throw new ArgumentException( "value must be of type Auto.", "value" ); } protected override void OnRemove( int index, Object value ) { if ( (value is Auto) != true ) throw new ArgumentException( "value must be of type Auto.", "value" ); } protected override void OnSet( int index, Object oldValue, Object newValue ) { if ( (newValue is Auto) != true ) throw new ArgumentException( "newValue must be of type Auto.", "newValue" ); } protected override void OnValidate( Object value ) { if ( (value is Auto) != true ) throw new ArgumentException( "value must be of type Auto." ); } } public class Autohaus { private Autos autos; [XmlElementAttribute(IsNullable = false)] public Autos Angebot { get { return autos; } set { this.autos = value; } } public Autohaus() { autos = new Autos(); } public Autohaus(Autos autos) { Angebot = autos; } }
-
so hat der Serialisierer genau ein Objekt (das Authaus)
welches seinerseits etliche Autos haben kann.
Das schöne daran wir haben uns ein spezialisierte Collection benutzt abgeletet!
Sicherlich kann man noch weiterrumfeilenaber ich habs getestet soviel ist sicher bei mir auf der Platte liegt genau eine XML-File
wo nen Autohaus mit 2 Fahrzeugen rumhullert!
-
so sieht das daraus resultierende XML aus als Rootelement benutzt er im uebrigen immer den Klassenamen soweit von dir nicht selbst vorgegeben.
<?xml version="1.0" encoding="us-ascii"?> <Autohaus xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Angebot><Marke>Ford</Marke><Autoname>Mustang</Autoname></Angebot> <Angebot><Marke>IFA</Marke><Autoname>Trabi</Autoname></Angebot> </Autohaus>
Fang dort bloss nicht an explizit selbst Dinge direkt uber den
XmlTextWriter reinzuschreiben das macht bei einem Fehler von Dir
die Datenhaltung inkonsistent, wodurch unter umständen die Deserialisierung des Objektes durch den XmlSerilaisierer (der beides kann) nicht mehr klappt!!!!!
-
was meine ich mit dem Hinweis:
sowas hier
XmlWriter xmlWriter = new XmlTextWriter("C:\\myAutoInfo.xml", null); xmlWriter.WriteStartElement("Automarken"); xmlWriter.WriteEndElement();
von
xmlWriter.WriteStartElement("Automarken"); xmlWriter.WriteEndElement();
bekommt der XmlSerialisierer nix mit lass es weg!
Instanz von Serialisierer machen
Instanz von XmlTextWriter machen
Objekt Serilisieren
Dem XmlTextWriter die Datei schliessen lassendas ist der Ablauf
-
kopier Dir meinen Code und probier es aus.
Ich hoffe ich konnte Erfahrung vermitteln!
Mit bestem Gruss sclearscreen
-
ncc-1701-m schrieb:
Hi versuchs mal so:
public class Automarken { private Auto[] m_Autos; public Auto[] Autos { get { return m_Autos; } set { m_Autos = value; } } }
und dann:
Auto[] autos = new Auto[]{ new Auto("mustang"), new Auto("lamborghini"), new Auto("mercedes")}; Automarken marken = new Automarken(); marken.Autos = autos try { // Serializer erstellen, der den Object-Type Automarke!! aufnimmt XmlSerializer serializer = new XmlSerializer(typeof(Automarken)); XmlWriter xmlWriter = new XmlTextWriter("C:\\myAutoInfo.xml", null); // Serialisieren serializer.Serialize(xmlWriter, marken); xmlWriter.Close(); }
oder eben so hmm es geht immer noch ein Tick einfacher
-
nee muss ich leider revidieren habs eben ausprobiert ein Array wird vom
XmlSerialisierer nicht als ein Objekt anerkannt!!!!Mann bekommt eine Exception wenn Du das Feld in einer klasse kapselst könnte es gehen!
Oder machst es so wie ich machst ne spezielle Collection und hälst das Ding in einer Klasse!
@leo_aka_qsch
Probier wenn Dir ein Feld lieber ist es in einer Klasse zu kapseln diese gibst du dem Serialisierer dann ist es ein Objekt! Oder nimm das kleine OOP-System siehe mein Beispiel-Code
-
ncc-1701-m schrieb:
Hi versuchs mal so:
public class Automarken { private Auto[] m_Autos; public Auto[] Autos { get { return m_Autos; } set { m_Autos = value; } } }
und dann:
Auto[] autos = new Auto[]{ new Auto("mustang"), new Auto("lamborghini"), new Auto("mercedes")}; Automarken marken = new Automarken(); marken.Autos = autos try { // Serializer erstellen, der den Object-Type Automarke!! aufnimmt XmlSerializer serializer = new XmlSerializer(typeof(Automarken)); XmlWriter xmlWriter = new XmlTextWriter("C:\\myAutoInfo.xml", null); // Serialisieren serializer.Serialize(xmlWriter, marken); xmlWriter.Close(); }
oh ich habe mal wieder Tomaten auf den Augen, jo es geht doch einfacher ich streu mir Asche aufs Haupt naja! Immerhin hat unser Besucher jetzt 2 Optionen an der Hand die funktionieren!
hast es ja gekapselt
-
man brauch in der Klasse die das Feld kapselt noch nen StandardKonstruktor
hier so isses funktionstüchtig die Klasse mit dem Feld
public class AutoMarken { private Auto [] autos; public AutoMarken(Auto [] autos) { this.autos = autos; } [XmlElementAttribute(IsNullable = false)] public Auto [] Autos { get { return autos; } set { this.autos = null; this.autos = new Auto [value.Length]; for(int i = 0;i< value.Length;i++) { this.autos[i] = value[i]; } } } public AutoMarken() { autos = null; } }
hmm XML kann so schön sein
-
[STAThread] static void Main(string[] args) { // // TODO: Fügen Sie hier Code hinzu, um die Anwendung zu starten // Auto auto1 = new Auto("Ford", "Mustang"); Auto auto2 = new Auto("IFA", "Trabi"); Autohaus autohaus = new Autohaus(); autohaus.Angebot.Add(auto1); autohaus.Angebot.Add(auto2); Auto [] cars = {auto1,auto2}; AutoMarken mengeautos = new AutoMarken(cars); try { XmlSerializer xmls1 = new XmlSerializer(typeof(Autohaus)); XmlSerializer xmls2 = new XmlSerializer(typeof(AutoMarken)); XmlTextWriter xmlw1 = new XmlTextWriter("C:\\autos1.xml",System.Text.Encoding.ASCII); xmls1.Serialize(xmlw1,autohaus); XmlTextWriter xmlw2 = new XmlTextWriter("C:\\autos2.xml",System.Text.Encoding.ASCII); xmls2.Serialize(xmlw2,mengeautos); xmlw1.Close(); xmlw2.Close(); } catch(System.Exception ex) { Console.WriteLine(ex.Message); } Console.ReadLine(); } } public class Auto { public Auto(string Marke, string Autoname) { this.Marke = Marke; this.Autoname = Autoname; } public Auto() { Marke = ""; Autoname = ""; } private string autoname; private string marke; [XmlElementAttribute(IsNullable = false)] public string Marke { get { return marke; } set { this.marke = value; } } public string Autoname { get { return autoname; } set { this.autoname = value; } } } public class Autos : CollectionBase { public Auto this[ int index ] { get { return( (Auto) List[index] ); } set { List[index] = value; } } public int Add( Auto value ) { return( List.Add( value ) ); } public int IndexOf( Auto value ) { return(List.IndexOf( value ) ); } public void Insert( int index, Auto value ) { List.Insert( index, value ); } public void Remove( Auto value ) { List.Remove( value ); } public bool Contains( Auto value ) { // If value is not of type Int16, this will return false. return( List.Contains( value ) ); } protected override void OnInsert( int index, Object value ) { if ( (value is Auto) != true ) throw new ArgumentException( "value must be of type Auto.", "value" ); } protected override void OnRemove( int index, Object value ) { if ( (value is Auto) != true ) throw new ArgumentException( "value must be of type Auto.", "value" ); } protected override void OnSet( int index, Object oldValue, Object newValue ) { if ( (newValue is Auto) != true ) throw new ArgumentException( "newValue must be of type Auto.", "newValue" ); } protected override void OnValidate( Object value ) { if ( (value is Auto) != true ) throw new ArgumentException( "value must be of type Auto." ); } } public class Autohaus { private Autos autos; [XmlElementAttribute(IsNullable = false)] public Autos Angebot { get { return autos; } set { this.autos = value; } } public Autohaus() { autos = new Autos(); } public Autohaus(Autos autos) { Angebot = autos; } } public class AutoMarken { private Auto [] autos; public AutoMarken(Auto [] autos) { this.autos = autos; } [XmlElementAttribute(IsNullable = false)] public Auto [] Autos { get { return autos; } set { this.autos = null; this.autos = new Auto [value.Length]; for(int i = 0;i< value.Length;i++) { this.autos[i] = value[i]; } } } public AutoMarken() { autos = null; } }
Viel Spass