0

I'm developing an app where I need to export documents based on different templates, I'm using PDFsharp and migradoc to create PDF documents. My documents have static text and there are just some parts that are generated dynamically. I think XML would be a good idea to this, however I'm new in this topic. I want to achieve something like this (static text and variables):

<xml>
<name> bla bla bla bla bla <date> bla bla bla blabla
<subject> bla bla bla bla
<fullname>
</xml>

What are your suggestions? Where should I start? Should I use XML?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Jorge Zapata
  • 2,316
  • 1
  • 30
  • 57
  • Just a note: the code you posted isn't actually well-formed XML. – svick Oct 17 '11 at 22:22
  • Maybe something [like this](http://stackoverflow.com/questions/159017/named-string-formatting-in-c-sharp) will be enough for you? – svick Oct 17 '11 at 22:24
  • But how to do this without having to put static text on my source code? – Jorge Zapata Oct 17 '11 at 23:23
  • Load the text from a config file. I thought that's what you wanted. Or maybe I misunderstood something? – svick Oct 17 '11 at 23:42
  • No svick you're right, I was thinking to give format to text from XML but maybe it would be better to do this from Migradoc options. Thanks! – Jorge Zapata Oct 18 '11 at 00:27

1 Answers1

0

If you want to use the XML format I would definitely use the XmlSerializer class of the .NET framework. Using it you just have to write serializable classes and don't have to carry about the export and import. Let me give you an example.

public class Document
{
  public string Name { get; set; }
  public string Subject { get; set; }

  public void Export(string path)
  {
    // you should use a try-catch-statement, that's just the way it works
    XmlSerializer serializer = new XmlSerializer(typeof(Document));
    TextWriter tr = new StreamWriter(path);
    serializer.Serialize(tr, this);
    tr.Close();
  }

  public static Document Import(string path)
  {
    // you should use a try-catch-statement, that's just the way it works
    XmlSerializer serializer = new XmlSerializer(typeof(Document));
    TextReader tr = new StreamReader(path);
    Document document = (Document)serializer.Deserialize(tr);
    tr.Close();
    return document;
  }
}

You can customize the tags of the exported XML document using attributes for your class and members. Have a look at the examples in this MSDN documentation.

MatthiasG
  • 4,434
  • 3
  • 27
  • 47
  • I think the point here isn't to de-/serialize some document. But to use human-written template (possibly formatted as XML) to format textual output. – svick Oct 17 '11 at 22:26
  • Oh, I see. Maybe I misunderstood the question. If so, just ignore my answer. But let's see how Jorge thinks about it. – MatthiasG Oct 17 '11 at 22:30
  • Thanks for your answer @MatthiasG however I'm looking something like svick wrote, a human written template – Jorge Zapata Oct 17 '11 at 23:22