c# - 以编程方式填充 XML "Template"C#

标签 c# xml templates jinja2 placeholder

我想做的是在 xml 中找到占位符并替换它们。 Jinja2 在 Python 中完成了此操作,但我正在寻找 C# 中的类似内容。基本上我想做的是:

<?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>

并以编程方式将其转换为:

<?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>

作为引用,我认为它应该如何工作的一个简单示例是:

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);

任何帮助都会很棒。谢谢!

最佳答案

我知道已经晚了,但我真的需要你在这里要求的东西,所以我做了这个 https://github.com/beto-rodriguez/Templator标记有点不同,但它应该工作相同,如果您熟悉 angularJs,您将不会有任何问题使用它。

我选择这种方法是因为我需要与更多用户共享模板,您生成一个模板并可以共享它来打印标签(在我的例子中)

举个例子

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 来源

<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>

已编译

<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>

您也可以从 Nuget 安装它:

Install-Package SuperXml

关于c# - 以编程方式填充 XML "Template"C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18710576/

相关文章:

c# - 调试控制台中的颜色

c# - 如何在 C# 中克隆通用列表?

c++ - 在 Qt 中从 xml 文件中添加/删除特定元素?

java - 如何从底部设计圆形 View ?

c++ - 创建一个成员数量可变的类型

c# - 如何从两个窗口访问一个类(C# WPF)

c# - 预加载程序集 : referenced, 未引用,直到需要时才加载

c++ - 使用 TinyXML 修改文档

c++ - 如何减少功能模板的特化程度?

HTML 模板框架可以让您在没有 javascript 的情况下包含部分内容?