What I am trying to do is find placeholders in an xml and replace them. Jinja2 did this in Python, but I am looking for something similar in C#. Essentially what I want to do is take this:
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Title>{{ myTitle }}</Title>
<Comp>
{% for item in compItems %} <CompItem>
<CompItemConfig>{{ item.config }}</CompItemConfig>
</CompItem>
</Comp>
{% endfor %}
</Data>
And programmatically turn it into:
<?xml version="1.0" encoding="utf-8"?>
<Data>
<Title>Brown Fox</Title>
<Comp>
<CompItem>
<CompItemConfig>QUICK</CompItemConfig>
</CompItem>
<CompItem>
<CompItemConfig>JUMPS</CompItemConfig>
</CompItem>
<CompItem>
<CompItemConfig>NOT LAZY</CompItemConfig>
</CompItem>
</Comp>
</Data>
For some reference, a quick example of how I think it should work would be:
Dictionary<string, string> myDictionary = new Dictionary<string, string>();
myDictionary.Add("myTitle", "Brown Fox");
myDictionary.Add("compItem", "QUICK");
myDictionary.Add("compItem", "JUMPS");
myDictionary.Add("compItem", "NOT LAZY");
FillTemplate("C:\myTemplate.xml", myDictionary);
Any help at all would be great. Thank you!
I know its late, but I really needed what you are asking for here, so I made this https://github.com/beto-rodriguez/Templator the markup is a little bit different but it should work the same, if you are familiar with angularJs you wont have any problems to use it.
I went for this method because I needed to share the template with more users, you generate a template and can share it to print labels (in my case)
here is an example
C#
var compiler = new Compiler()
.AddKey("name", "Excel")
.AddKey("width", 100)
.AddKey("height", 500)
.AddKey("bounds", new[] {10, 0, 10, 0})
.AddKey("elements", new []
{
new { name = "John", age= 10 },
new { name = "Maria", age= 57 },
new { name = "Mark", age= 23 },
new { name = "Edit", age= 82 },
new { name = "Susan", age= 37 }
});
var compiled = compiler.CompileXml(@"C:\...\myXml.xml")
XLM Source
<document>
<name>my name is {{name}}</name>
<width>{{width}}</width>
<height>{{height}}</height>
<area>{{width*height}}</area>
<padding>
<bound sxRepeat="bound in bounds">{{bound}}</bound>
</padding>
<content>
<element sxRepeat="element in elements" sxIf="element.age > 25">
<name>{{element.name}}</name>
<age>{{element.age}}</age>
</element>
</content>
</document>
Compiled
<document>
<name>my name is Excel</name>
<width>100</width>
<height>500</height>
<area>50000</area>
<padding>
<bound>10</bound>
<bound>0</bound>
<bound>10</bound>
<bound>0</bound>
</padding>
<content>
<element>
<name>Maria</name>
<age>57</age>
</element>
<element>
<name>Edit</name>
<age>82</age>
</element>
<element>
<name>Susan</name>
<age>37</age>
</element>
</content>
</document>
you can install it from Nuget too:
Install-Package SuperXml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With