Hallo Kollegen,
ich finde einfach keine Lösung im Inet. Ich möchte einfach Ein Objekt, welches u.a. eine Liste von Unterobjekten enthält mit dem XML-Serialisierer serialisieren. Muss man hier wirklich aus der Liste ein Array machen?
Hier mal ein Beispiel
[XmlRoot("site") ]
public class NavigationSite {...
[XmlElement("process")]
public IList<NavigationProcess> Processes { ...
wie würdet ihr dieses Problem am saubersten lösen?
Ich möchte dann in der XML-Datei stehen haben:
<?xml version="1.0" encoding="UTF-8" ?>
<site source="https://bwin.com" visible="true" shell="false" />
<process>...
</process>
<process>...
</process>
</site>
Also n-verschiedene process-Elemente.
Danke schon mal für eine etwaige Antwort. Ein guter Link wäre auch super.
Nö, sollte gehen.
Habe schon folgendes gemacht :
[XmlArray("Elemente")]
[XmlArrayItem("Text", typeof(layText))]
[XmlArrayItem("Trenner", typeof(layTrenner))]
[XmlArrayItem("Item", typeof(layIOItem))]
public List<layItem> Items
{
get { return m_items; }
set { m_items = value; }
}
@a957m,
Hm .. dann mache ich aber was falsch. Kannst Du mir da helften?
Code:
XmlSerializer xmlSerializer = new XmlSerializer(site.GetType());
XmlTextWriter xmlTextWriter = new XmlTextWriter(@"C:\Test.xml", Encoding.UTF8);
xmlTextWriter.Formatting = Formatting.Indented;
xmlSerializer.Serialize(xmlTextWriter, site);
xmlTextWriter.Close();
In der Klasse NavigationSite ....
/// <summary>
/// Property Processes
/// </summary>
[XmlArray]
[XmlArrayItem("process", typeof(NavigationProcess))]
public IList<NavigationProcess> Processes {
get {
return processes;
}
}
.. und in der Klasse NavigationProcess habe ich dann ...
[XmlType]
public class NavigationProcess { ...
Die Liste ist gefüllt, jedoch schreibt der mir nur raus:
<?xml version="1.0" encoding="utf-8" ?>
<site xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" shell="false" visible="true" />
X( .. die Liste wird also kommentarlos ignoriert. Da muss es doch zig Beispiellinks geben, sowas kommt doch fast in jeder XML-Datei vor. Was mache ich falsch?
OK, hat sich erst mal erledigt, ich habe im Netz was gefunden und habe das mit den Arrays verwendet, das sieht dann so aus:
[XmlElement("process")]
public NavigationProcess[] ProcessesArray {
get {
NavigationProcess[] navigationProcess = new NavigationProcess[this.Processes.Count];
this.processes.CopyTo(navigationProcess, 0);
return navigationProcess;
}
set {
if(value != null) {
NavigationProcess[] navigationProcesses = (NavigationProcess[])value;
this.Processes.Clear();
foreach(NavigationProcess navigationProcess in navigationProcesses)
this.Processes.Add(navigationProcess);
}
}
}
.. und funzzt bidirektional ... finde ich jetzt zwar nicht doll, aber es funzzt.