NetworkComponent 클래스의 _updatedComponents라는 객체가있는 배열이 있습니다. 루트 요소 (= 배열)의 이름과 네임 스페이스가 변경되고 개별 NetworkComponent-item의 이름이 구성 요소로 변경되는 방식으로 직렬화해야합니다. 예외가 발생하는 아래 코드가 있습니다.배열을 serialize 할 때 XmlAttributeOverrides를 사용하는 방법은 무엇입니까?
System.InvalidOperationException : 'ComponentSyncService.NetworkComponent []'형식을 반영하는 동안 오류가 발생했습니다. System.InvalidOperationException ---> : XmlRoot 및 XmlType 특성을 ComponentSyncService.NetworkComponent [] 유형에 지정할 수 없습니다.
코드 :
이_updatedComponents
무엇
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);
클래스를 생성 했으므로 System.Xml.Serialization.XmlTypeAttribute 정의를 변경하지 않으려 고하므로 다시 생성 할 때 변경 내용이 손실됩니다. –