0

I have a tricky question regarding shallow serialization of List of objects

my entities

public class Requests
{
    public string Action { get; set; }
    public Meta MetaType { get; set; }

}

public class Meta
{
    public string MerchantId { get; set; }
    public string IpAddress { get; set; }
    public string Version { get; set; }
}

Shallow serialization using XMLSerializer

    List<Requests> lstXML = new List<Requests>();

    Requests xml = new Requests();
    xml.Action = "INSERT";
    xml.MetaType = new Meta { IpAddress = "192.2.3.4", MerchantId = "DALE", Version = "1" };
    lstXML.Add(xml);
    xml = new Requests();
    xml.Action = "UPDATE";
    xml.MetaType = new Meta { IpAddress = "192.2.3.40", MerchantId = "SECD", Version = "1" };
    lstXML.Add(xml);


    using (var sw = new StreamWriter(@"C:\XML\test.txt"))
    {
        var serializer = new XmlSerializer(typeof(List<Requests>));
        serializer.Serialize(sw, lstXML);

    }

output textfile text.txt

  <?xml version="1.0" encoding="utf-8"?>
 <ArrayOfRequests xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                  xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <Requests>
  <Action>INSERT</Action>
   <MetaType>
  <MerchantId>DALE</MerchantId>
  <IpAddress>192.2.3.4</IpAddress>
  <Version>1</Version>
</MetaType>
   </Requests>
 <Requests>
<Action>UPDATE</Action>
<MetaType>
  <MerchantId>SECD</MerchantId>
  <IpAddress>192.2.3.40</IpAddress>
  <Version>1</Version>
</MetaType>
     </Requests>
    </ArrayOfRequests>

now my problem is

1) I need to remove the < ?XML version="1.0" ....> and the < ArrayOfRequests ...> tag and retain only the XML tag of my Entities. How can I do that?

2) How can I CAPITALIZE(CAPS) the Element Name ( -> ) in the output textfile?

my desired textfile output would be

<XML>
 <REQUESTS>
 <ACTION>INSERT</ACTION>
 <META>
   <MERCHANTID>DALE</MERCHANTID>
   <IPADDRESS>202.164.178.163</IPADDRESS>
   <VERSION>1</VERSION>
 </META>
 <REQUESTS>

 <REQUESTS>
 <ACTION>INSERT</ACTION>
 <META>
   <MERCHANTID>DALE</MERCHANTID>
   <IPADDRESS>202.164.178.163</IPADDRESS>
   <VERSION>1</VERSION>
 </META>
 <REQUESTS>

 </XML>

Thanks in advance guys! =)

CSharpNoob
  • 1,271
  • 7
  • 18
  • 27

3 Answers3

3

Try

[XmlType("REQUESTS")]  
public class Requests
{
    [XmlElement(ElementName="ACTION")]
    public string Action { get; set; }

    [XmlElement(ElementName="META")]
    public Meta MetaType { get; set; }
    ...

}

public class Meta
{

    [XmlElement(ElementName="MERCHANTID")]
    public string MerchantId { get; set; }
    ...
}

and

var serializer = new XmlSerializer(typeof(List<Requests>), new XmlRootAttribute("XML"));
Bala R
  • 107,317
  • 23
  • 199
  • 210
1

So, you don't want XML serialization. You want a custom approximation of it. Therefore, you're probably going to have to write it yourself. Are you using any kind of templating language? If you know the types that you'll be serializing up-front, a templating system will help you immensely. If you don't know the types beforehand, next answer please.

Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
1

The absolute simplest solution would probably to process the string, as currently generated, with a couple of post-processing steps:

// Stream to a string instead of directly to the file
yourString = // stream XML here.

// Remove the xmlns stuff.
yourString = yourString.Replace(
          " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", 
          ""); 

// Upper-case the start-elements and end-elements (your data has no attributes, 
// so keep it simple)
yourString = Regex.Replace(yourString, 
                           "<\{0,1}([^>]+)>", 
                           delegate(Match m) 
                           {
                               return m.Value.ToUpper(); 
                           });
Ray Hayes
  • 14,896
  • 8
  • 53
  • 78
  • thanks for answering the 2nd question, I also think its the only solution since XmlSerializer automatically creates the xmlns captions... – CSharpNoob Mar 26 '11 at 15:57