Convert object to xml

Posted by Suneel161 under Regular Expressions on 5/31/2011 | Points: 10 | Views : 1590 | Status : [Member] | Replies : 1
hi


i am converting object to xml.i am getting following output.

<TentativeTicketV2>
<blockDuration>123</blockDuration>
<boardingPointId>123</boardingPointId>
<delayedPayment>true</delayedPayment>
<journeyLegId>123</journeyLegId>
<seatDetails>
<seatDetail>
<seatDetail>
<gender>m</gender>
<isPrime>true</isPrime>
<passengerMobile>1234567890<passengerMobile />
<passengerName>abc<passengerName />
<seatName>23</seatName>
</seatDetail>
</seatDetail>
</seatDetails>
</TentativeTicketV2>

but how to remove seatdetail root element.

like

<TentativeTicketV2>
<blockDuration>123</blockDuration>
<boardingPointId>123</boardingPointId>
<delayedPayment>true</delayedPayment>
<journeyLegId>123</journeyLegId>
<seatDetails>
<seatDetail>
<gender>m</gender>
<isPrime>true</isPrime>
<passengerMobile>1234567890<passengerMobile />
<passengerName>abc<passengerName />
<seatName>23</seatName>
</seatDetail>
</seatDetails>
</TentativeTicketV2>




Responses

Posted by: Lakn2 on: 5/31/2011 [Member] Starter | Points: 25

Up
0
Down
try this

yes. Using System.Xml.Serialization.XmlSerializer. Here is some sample code:
using System;
using System.IO;
using System.Xml.Serialization;

namespace Holmok.Utilities
{
public class serial
{
public static void ObjectToXml(object obj, string path_to_xml)
{
//serialize and persist it to it's file
try
{
XmlSerializer ser = new XmlSerializer(obj.GetType());
FileStream fs = File.Open(
path_to_xml,
FileMode.OpenOrCreate,
FileAccess.Write,
FileShare.ReadWrite);
ser.Serialize(fs,obj);
}
catch (Exception ex)
{
throw new Exception(
"Could Not Serialize object to " + path_to_xml,
ex);
}
}
}
}.

Thanks&Regards
LakshmiNarayana Nalluri.

Suneel161, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response