2010-02-11 21 views
6

मेरे पास क्लास 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); 
+0

मैं जोड़ सकता हूं कि क्लास जेनरेट होने के बाद से मैं System.Xml.Serialization.XmlTypeAttribute परिभाषाओं को बदलना नहीं चाहता हूं और इस प्रकार आपके उत्तर के लिए –

उत्तर

8

_updatedComponents क्या है? मुझे लगता है कि यह NetworkComponent[] है - जो चीजों को बहुत मुश्किल बना देगा। मैं एक रैपर प्रकार लिखने का सुझाव दूंगा:

public class ComponentsMessage { 
    public NetworkComponent[] Components {get;set;} 
} 

फिर आप सही विशेषताओं को जोड़ सकते हैं। यदि आपको NetworkComponent पर विज्ञापन-प्रसार विशेषताओं का समर्थन करने की आवश्यकता है तो आपको अभी भी विशेषता-ओवरराइड का उपयोग करने की आवश्यकता होगी (इसलिए मैंने उपर्युक्त सजाया नहीं है), लेकिन ComponentsMessage गुणों को लेने में प्रसन्न होना चाहिए।

वैकल्पिक रूप से, बस एक अलग डीटीओ लिखें और मूल्यों को मैप करें।

यदि यह सरल है, तो आप सिर्फ उपयोग करने में सक्षम हो सकता है:

[XmlRoot("components", Namespace = XmlNamespace)] 
[XmlType("components", Namespace = XmlNamespace)] 
public class ComponentsMessage 
{ 
    public const string XmlNamespace = "http://www.example.com/nis/componentsync"; 
    [XmlElement("component")] 
    public NetworkComponent[] Components { get; set; } 
} 

वैकल्पिक रूप से, अगर आप चाहिए उपयोग विशेषता-ओवरराइड, मैं अभी भी एक आवरण वस्तु का उपयोग करेंगे:

public class ComponentsMessage 
{ 
    public NetworkComponent[] Components { get; set; } 
} 
class Program 
{ 
    static void Main() 
    { 
     NetworkComponent[] _updatedComponents = new NetworkComponent[2] { 
      new NetworkComponent{},new NetworkComponent{} 
     }; 
     const string XmlNamespace = "http://www.example.com/nis/componentsync"; 
     XmlAttributeOverrides ao = new XmlAttributeOverrides(); 
     ao.Add(typeof(ComponentsMessage), new XmlAttributes { 
      XmlRoot = new XmlRootAttribute("components") { Namespace = XmlNamespace }, 
      XmlType = new XmlTypeAttribute("components") { Namespace = XmlNamespace } 
     }); 
     ao.Add(typeof(ComponentsMessage), "Components", new XmlAttributes { 
      XmlElements = { 
       new XmlElementAttribute("component") 
      } 
     }); 
     ComponentsMessage msg = new ComponentsMessage { Components = _updatedComponents }; 
     XmlSerializer serializer = new XmlSerializer(msg.GetType(), ao); 
     serializer.Serialize(Console.Out, msg); 
    } 
} 
+0

पुन: उत्पन्न होने पर परिवर्तन खो जाएंगे! जैसा कि मैंने अपने प्रश्न में बताया है: "मेरे पास ऑब्जेक्ट्स के _updatedComponents नामक एक सरणी है जो क्लास NetworkComponent के हैं।" मैं सिर्फ काम छोड़ रहा हूं इसलिए मैं कल कोशिश करूँगा। बहुत बुरा है कि यह मुश्किल है। मैं पूरी तरह से समझ में नहीं आया क्यों ... –

+0

फिर से धन्यवाद! मैं सुबह में आपका संपादित उत्तर 1 चीज़ की जांच करूंगा। –

+0

मैंने सरल संस्करण की कोशिश की और यह काम किया। आपका बहुत बहुत धन्यवाद! आपने XmlAttributeOverrides संस्करण भी प्रदान करने के लिए बहुत प्रयास किए हैं। उम्मीद है कि यह किसी और की मदद करेगा जिसकी वास्तव में इसकी आवश्यकता है। –

संबंधित मुद्दे

 संबंधित मुद्दे