Mam tablicę o nazwie _updatedComponents obiektów, które są klasy NetworkComponent. Muszę serializować go w taki sposób, że zmieni się nazwa i przestrzeń nazw elementu głównego (= tablica) i nazwa pojedynczego elementu NetworkComponent zostanie zmieniona na składnik. Mam kod poniżej, który powoduje wyjątek:jak korzystać z XmlAttributeOverrides podczas serializacji tablicy?
System.InvalidOperationException: Wystąpił błąd odzwierciedlający typ "ComponentSyncService.NetworkComponent []". ---> System.InvalidOperationException: Atrybuty XmlRoot i XmlType nie mogą być określone dla typu ComponentSyncService.NetworkComponent [].
Kod:
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);
Mogę dodać, że nie chcę zmieniać definicji System.Xml.Serialization.XmlTypeAttribute, ponieważ klasa jest generowana, a zatem zmiany zostaną utracone po zregenerowaniu –