मेरे पास क्लास NetworkComponent की ऑब्जेक्ट्स के _updatedComponents नामक एक सरणी है। मुझे इसे इस तरह से क्रमबद्ध करना है कि रूट तत्व (= सरणी) का नाम और नामस्थान बदल दिया गया है और व्यक्तिगत नेटवर्ककंपोनेंट-आइटम का नाम घटक में बदल दिया गया है। मेरे पास एक कोड है जो अपवाद का कारण बनता है:सरणी को क्रमबद्ध करते समय XmlAttributeOverrides का उपयोग कैसे करें?
सिस्टम। अविश्वसनीय अपवाद अपवाद: 'ComponentSyncService.NetworkComponent []' को प्रतिबिंबित करने में त्रुटि हुई। ---> System.InvalidOperationException: XmlRoot और XmlType विशेषताओं को ComponentSyncService.NetworkComponent [] प्रकार के लिए निर्दिष्ट नहीं किया जा सकता है।
कोड:
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 परिभाषाओं को बदलना नहीं चाहता हूं और इस प्रकार आपके उत्तर के लिए –