2011-06-23 9 views
17

क्रमबद्ध करते समय तत्वों के क्रम को बदलें, मुझे एक्सएमएल और बैक पर ऑब्जेक्ट को क्रमबद्ध करने की आवश्यकता है। एक्सएमएल ठीक है और मैं इसे बदल नहीं सकता। मैं bookingList के बाद इस संरचना को उत्पन्न करने में विफल रहता हूं।एक्सएमएल

मैं कैसे "समूह" इन <booking> तत्वों एक सूची के रूप में प्रकट करने के लिए और <booking> तत्वों की इस सूची से पहले <error> & <counter> रख सकते हैं।

मेरे यहाँ उदाहरण देखें:

संरचना मैं की जरूरत है ....

<nicexml> 
<key_id>1234567</key_id> 
<surname>Jil</surname> 
<name>Sander</name> 
<station_id>1</station_id> 
<ownBookings> 
    <bookingList> 
     <error></error> 
     <counter>20</counter> 
     <booking> 
      <bookingID>1234567890</bookingID> 
     </booking> 
     <booking> 
      <bookingID>2345678901</bookingID> 
     </booking> 
    </bookingList> 
</ownBookings> 
</nicexml> 

संरचना मैं नीचे सी # कोड के साथ मिलता है ....

<nicexml> 
<key_id>1234567</key_id> 
<surname>Jil</surname> 
<name>Sander</name> 
<station_id>1</station_id> 
<ownBookings> 
    <bookingList> 
      <booking> 
     <booking> 
      <bookingID>1234567890</bookingID> 
     </booking> 
     <booking> 
      <bookingID>2345678901</bookingID> 
     </booking> 
      <booking> 
     <error></error> 
     <counter>20</counter> 
    </bookingList> 
</ownBookings> 
</nicexml> 

सी # कोड:

using System; 
using System.Xml.Serialization; 
using System.Collections.Generic; 

namespace xml_objects_serials 
{ 
    public class bookings 
    { 
     public class nicexml 
     { 
      public string key_id 
      { get; set; } 

      public string surname 
      { get; set; } 

      public string name 
      { get; set; } 

      public int station_id 
      { get; set; } 

      public ownBookings ownBookings 
      { get; set; } 

     } 

     public class ownBookings 
     { 
      public bookingList bookingList 
      { get; set; } 

     } 
     public class bookingList { 
      public string error 
      { get; set; } 
      public int counter 
      { get; set; } 
      public List<booking> booking= new List<booking>(); 
     } 

     public class booking 
     { 
      public int bookingID 
      { get; set; } 
     } 
    } 

उत्तर

25

समर्थक को सजाने का प्रयास करें वर्ग के साथ के लिए पर नियंत्रण करें कि उस वर्ग की ऑब्जेक्ट्स को XML पर क्रमबद्ध करने के लिए कैसे नियंत्रित किया जा रहा है।

यहाँ एक उदाहरण है:

public class bookingList 
{ 
    [XmlElement(Order = 1)] 
    public string error { get; set; } 
    [XmlElement(Order = 2)] 
    public int counter { get; set; } 
    [XmlElement(ElementName = "booking", Order = 3)] 
    public List<booking> bookings = new List<booking>(); 
} 

public class booking 
{ 
    public int id { get; set; } 
} 

अपने परीक्षण में मैं इस उत्पादन प्राप्त:

<?xml version="1.0" ?> 
<bookingList> 
    <error>sample</error> 
    <counter>0</counter> 
    <booking> 
     <id>1</id> 
    </booking> 
    <booking> 
     <id>2</id> 
    </booking> 
    <booking> 
     <id>3</id> 
    </booking> 
</bookingList> 

संबंधित संसाधन:

+0

thx ... तत्व आदेश ... मैं इसे msdn से क्यों नहीं मिला .... धन्यवाद –

+0

मुझे इस "तत्व" के बारे में पता नहीं था। आपका बहुत बहुत धन्यवाद! –

-3

मुझे इस समस्या का सामना करना पड़ रहा था और मैंने इसे हल किया ... अच्छा यह बहुत रोचक है, और यह शायद .net में एक बग है।

समस्या यहाँ है: public List<booking> booking= new List<booking>();

आप उपयोग करना चाहिए: public List<booking> booking { get; set; }

और तुम परिभाषित आदेश मिल जाएगा .... लेकिन क्यों? कौन जानता है ... :)