6

This is what I did:

A Serializable class:

[Serializable()]
public class Ticket
{
    public string   CitationNumber { get; set; }
    public decimal Amount { get; set; }
}

Then serialize a model into xml:

var model = cart.Citations
    .Select(c => new Ticket(c.Number, c.Amount)).ToList();
var serializer = new XmlSerializer(typeof (List<Ticket>));
var sw = new StringWriter();
serializer.Serialize(sw, model);
return sw.ToString();

The output sw.ToString() is like

<?xml version="1.0" encoding="utf-16"?>
<ArrayOfTicket xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Ticket>
<CitationNumber>00092844</CitationNumber>
<Amount>20</Amount>
</Ticket>
</ArrayOfTicket>  

Is there a way to customize the Serialize() output to remove those schema info like: <?xml version="1.0" encoding="utf-16"?> and xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"?

And how can I change the root element ArrayOfTicket into something else?

Do I have control with those output?

Blaise
  • 21,314
  • 28
  • 108
  • 169
  • It saves into the database and I really don't need that extra information. – Blaise Aug 12 '14 at 20:55
  • I don't think that will make a difference, even in the long run. Also why not store each item (CitationNumber, Amount, ...) separately as it's own table instead of storing the xml in the database? You can't easily query stored xml. – gunr2171 Aug 12 '14 at 20:57
  • have you thought about converting the data into an DataTable then doing it from there..I have done this several times and If you like I can send you a working example. for my case I am reading data from a .csv file then loading it into a `DataTable` from there I serialize out to XML using the `MissingSchemaAction` – MethodMan Aug 12 '14 at 20:58

1 Answers1

12

You need a few xml tricks...

var serializer = new XmlSerializer(typeof(List<Ticket>));

var ns = new XmlSerializerNamespaces();
ns.Add("", "");
var sw = new StringWriter();
var xmlWriter = XmlWriter.Create(sw, new XmlWriterSettings() { OmitXmlDeclaration = true });

serializer.Serialize(xmlWriter, model, ns);
string xml = sw.ToString(); 

Output:

<ArrayOfTicket>
  <Ticket>
    <CitationNumber>a</CitationNumber>
    <Amount>1</Amount>
  </Ticket>
  <Ticket>
    <CitationNumber>b</CitationNumber>
    <Amount>2</Amount>
  </Ticket>
</ArrayOfTicket>

PS: I added Indent = true to XmlWriterSettings to get the above output

L.B
  • 114,136
  • 19
  • 178
  • 224
  • Great answer. That is exactly what I need. And is it possible to change the default root node `ArrayOfTicket`? – Blaise Aug 12 '14 at 21:08
  • @Blaise Although I don't like "people extending their question with new questions" (when the original question is answered), this is how you can do it. `var serializer = new XmlSerializer(typeof(List), new XmlRootAttribute("Tickets"));` – L.B Aug 12 '14 at 21:11
  • I probably did ask for too much in one question. But thanks a lot. – Blaise Aug 12 '14 at 21:13
  • @L.B `serializer.Serialize(xmlWriter, model, ns);` What is model here, as When I run this code I'm getting a cast error – Arijit Mukherjee Aug 13 '14 at 07:03
  • @ArijitMukherjee See the question. It is `List` – L.B Aug 13 '14 at 07:05
  • @L.B Sorry My Ignorance , but I'm unable to find the solution of my problem , as when I'm passing my string it gives illegal characters in path – Arijit Mukherjee Aug 13 '14 at 07:17
  • As I already have the XML and want deserialize the same into my class – Arijit Mukherjee Aug 13 '14 at 07:21