以编程方式填充XML“模板”C#
发布时间:2020-12-15 23:57:10 所属栏目:百科 来源:网络整理
导读:我想要做的是在xml中找到占位符并替换它们. Jinja2在 Python中做到了这一点,但我在C#中寻找类似的东西.基本上我想做的是拿下这个: ?xml version="1.0" encoding="utf-8"?Data Title{{ myTitle }}/Title Comp {% for item in compItems %} CompItem CompItem
我想要做的是在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,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 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |