J'ai un tableau appelé _updatedComponents d'objets de la classe NetworkComponent. Je dois le sérialiser de la façon dont le nom et l'espace de noms de l'élément racine (= tableau) est changé et le nom de l'élément NetworkComponent individuel est changé en composant. J'ai un code ci-dessous qui provoque une exception:comment utiliser XmlAttributeOverrides lors de la sérialisation d'un tableau?
System.InvalidOperationException: Il y avait un type réfléchissant d'erreur 'ComponentSyncService.NetworkComponent []. ---> System.InvalidOperationException: Les attributs XmlRoot et XmlType ne peuvent pas être spécifiés pour le type ComponentSyncService.NetworkComponent [].
code:
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);
je pourrais ajouter que je ne veux pas changer les définitions de System.Xml.Serialization.XmlTypeAttribute puisque la classe est générée et ainsi les changements seront perdus une fois régénérés –