开发者

Filling own template files

What is the best way to fill a template file with variables, which are collected from a Windows Form or Web Page. I want to create a wizard program that collects some data from user, for instance, reportname, reporttype, etc. and I need to create then some开发者_开发问答 xml files (templates) with these variables inserted.


Well, first you need to save those variables somewhere, like a class or a Hashtable.
Then you load the xml file.

  XmlDocument doc = new XmlDocument();
  doc.Load("template.xml");

Then for each variable you select the proper node (using xpath) and change the inner contents to the variable value, like this:

  XmlNode node = xmlDoc.SelectSingleNode("Root/Node/Element");  
  node.InnerText="newValue"

The best way to make this all automatic would be saving both xpaths and the variables names into a dictionary/hashtable and these use it to lookup where a variable goes.

You would then save the file.

  doc.Save("result.xml");

Note: Ask for more information regarding the mapping if you didn't understand it.


One possibility would be to save the variables in an XML-Document (structure is up to you) and the use XSTL to transform it into your target XML file.

If you only need to insert a few variables, you could also define some custom placeholder format (e.g. '$XXX$' or even '{0}'...) and use simple string replacement.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜