Tengo una matriz llamada _updatedComponents de objetos que son de clase NetworkComponent. Tengo que serializarlo de la misma manera en que cambian el nombre y el espacio de nombres del elemento raíz (= matriz) y el nombre individual del elemento NetworkComponent se cambia a componente. Tengo un código a continuación que causa una excepción:cómo usar XmlAttributeOverrides al serializar una matriz?
System.InvalidOperationException: Hubo un error que refleja el tipo 'ComponentSyncService.NetworkComponent []'. ---> System.InvalidOperationException: los atributos XmlRoot y XmlType no se pueden especificar para el tipo ComponentSyncService.NetworkComponent [].
Código:
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);
podría agregar que no quiero cambiar las definiciones System.Xml.Serialization.XmlTypeAttribute ya que la clase se genera y por lo tanto los cambios se perderán cuando se regenere –