Ho un array chiamato _updatedComponents di oggetti che sono di classe NetworkComponent. Devo serializzarlo nel modo in cui il nome e lo spazio dei nomi dell'elemento root (= array) vengono modificati e il singolo NetworkComponent-il nome dell'elemento viene modificato in componente. Ho un codice di seguito che causa un'eccezione:come utilizzare XmlAttributeOverrides durante la serializzazione di un array?
System.InvalidOperationException: C'è stato un errore che riflette il tipo 'ComponentSyncService.NetworkComponent []'. ---> System.InvalidOperationException: gli attributi XmlRoot e XmlType potrebbero non essere specificati per il tipo ComponentSyncService.NetworkComponent [].
Codice:
XmlAttributeOverrides xaos = new XmlAttributeOverrides();
// the array itself aka the root. change name and namespace
XmlElementAttribute xea = new XmlElementAttribute(_updatedComponents.GetType());
xea.Namespace = "http://www.example.com/nis/componentsync";
xea.ElementName = "components";
XmlAttributes xas = new XmlAttributes();
xas.XmlElements.Add(xea);
xaos.Add(_updatedComponents.GetType(), xas);
// then the items of the array. just change the name
xea = new XmlElementAttribute(typeof(networkcomponent));
xea.ElementName = "component";
xas = new XmlAttributes();
xas.XmlElements.Add(xea);
xaos.Add(typeof(NetworkComponent), "NetworkComponent", xas);
XmlSerializer serializer = new XmlSerializer(_updatedComponents.GetType(), xaos);
XmlTextWriter writer = new XmlTextWriter(string.Format("{0}\\ComponentSyncWS_{1}.xml",
Preferences.FileSyncDirectory, requestId), Encoding.UTF8);
serializer.Serialize(writer, _updatedComponents);
Potrei aggiungere che non voglio modificare le definizioni System.Xml.Serialization.XmlTypeAttribute dal momento in cui la classe viene generata e quindi le modifiche andranno perse quando rigenerate –