从文本或xml文件自动生成代码然后编译?

发布于 2024-11-24 04:46:38 字数 702 浏览 3 评论 0原文

我想知道如何将特定格式的文本文件生成代码到 VB.net 或 C# 源文件中。 例如:我希望我的代码生成程序读取具有以下格式的文本文件:

<category1>
      <subcategory>
        entry1
        entry2
      </subcategory>
</Category1>

然后在 vb.net(或 C#)中生成代码:

Dim TreeNode1 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("entry1") 
Dim TreeNode2 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("entry2") 
Dim TreeNode3 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("subcategory", New System.Windows.Forms.TreeNode() {TreeNode1, TreeNode2})

这个想法是在用户修改 txt 文件并使用后编译主代码代码生成程序。我更喜欢用 C、Python 或 C# 编写代码生成程序。 我该怎么办?

I would like to know how I could generate code from a text file in a particular format into a VB.net or C# source file.
For instance: I would like my code generating program to read text file having the following format:

<category1>
      <subcategory>
        entry1
        entry2
      </subcategory>
</Category1>

And then generate code in vb.net(or C#):

Dim TreeNode1 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("entry1") 
Dim TreeNode2 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("entry2") 
Dim TreeNode3 As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("subcategory", New System.Windows.Forms.TreeNode() {TreeNode1, TreeNode2})

The idea is to compile the main code after the user have modified the txt file and used the code generating program. I would prefer to write the code generating program in C, python or C#.
How can I go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

吻安 2024-12-01 04:46:39

您需要编写解析器来解析您的文本文件。一旦解析器生成类似于 VB.net 或 C# 代码的字符串,您可以使用 Emit 编译为临时程序集

You need to write parser to parse your text file. Once parser generate string which is similar to VB.net or C# code, you can use Emit to compile to a temporary assembly

饮惑 2024-12-01 04:46:39

使用 CSharpCodeProvider,设置一个字符串骨架(您的其余部分),解析您的文件,然后将生成的代码注入到正确的位置。

有关 CSharpCodeProvider 的信息

请注意,您需要在内存中构建并将构建可执行文件设置为 false。 此处 是有关如何执行此操作的示例,它还演示了如何将编译器的程序集设置为当前执行文件的程序集。

我知道这有点模糊,但如何设置实际的 CSharpCodeProvider 取决于您想要做什么。另请注意,如果您收到 FileNotFound 异常,则会出现编译错误,顶部链接提供了一个很好的解决方案,说明如何以可读的方式抛出它们。

Use the CSharpCodeProvider, set up a string skeleton that is the rest of you class, parse your file, and then inject your generated code in the correct location.

Info about the CSharpCodeProvider

Note that you will want to build in memory and set build executable to false. Here is an example on how to do that, it also shows how set the assemblies for the compiler to the assemblies of the currently executing file.

I know this is a bit vague but how you set up the actual CSharpCodeProvider depends on what you are trying to do. Also beware that if you get FileNotFound exceptions you are getting compile errors, the top link gives a nice solution on how to throw them in a readable fashion.

墨小沫ゞ 2024-12-01 04:46:39

这取决于您最熟悉其中提到的哪种语言。我建议使用 python,因为您可以立即从命令行解释器开始使用它。

有用于 xml 解析的标准 library

from xml.dom.minidom import parse, parseString

dom1 = parse('c:\\temp\\mydata.xml') # parse an XML file by name

然后您可以迭代元素按名称,如下所示:

for node in dom1.getElementsByTagName('category1'):
    ...

或直接针对所有元素:

for node in dom1.childNodes:
   ...

下面是如何检查对象树的命令行解释器记录(>>> 代表解释器提示符):

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dom = parseString ('<root>contents</root>')
>>> dom.childNodes
[<DOM Element: root at 0x7f0d4519f680>]
>>> dom.childNodes[0]
<DOM Element: root at 0x7f0d4519f680>
>>> dom.childNodes[0].childNodes
[<DOM Text node "u'contents'">]
>>> dom.childNodes[0].childNodes[0]
<DOM Text node "u'contents'">
>>> dom.childNodes[0].childNodes[0].nodeValue
u'contents'
>>> 

This depends on which of mentioned languages is most familiar to you. I'd recommend to go with python, because you can start playing with it right away from command line interpreter.

There is standard library for xml parsing:

from xml.dom.minidom import parse, parseString

dom1 = parse('c:\\temp\\mydata.xml') # parse an XML file by name

Then you iterate through elements, either by name, like this:

for node in dom1.getElementsByTagName('category1'):
    ...

or straight-forward for all elements:

for node in dom1.childNodes:
   ...

Below is the command line interpreter transcript of how to inspect the object tree (>>> stand for interpreter prompt):

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:57:41) 
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> dom = parseString ('<root>contents</root>')
>>> dom.childNodes
[<DOM Element: root at 0x7f0d4519f680>]
>>> dom.childNodes[0]
<DOM Element: root at 0x7f0d4519f680>
>>> dom.childNodes[0].childNodes
[<DOM Text node "u'contents'">]
>>> dom.childNodes[0].childNodes[0]
<DOM Text node "u'contents'">
>>> dom.childNodes[0].childNodes[0].nodeValue
u'contents'
>>> 
夜巴黎 2024-12-01 04:46:38

我不太相信这是一个 python 问题,尽管问题中有标签和倒数第二句话,但这是一个 python 答案。

>>> from xml.etree import ElementTree as etree
>>> corpus = '''<category1>
...       <subcategory>
...         entry1
...         entry2
...       </subcategory>
... </category1>
... '''
>>> doc = etree.fromstring(corpus)
>>> for subcategory in doc.getchildren():
...     for entry in filter(bool,
...                         map(str.strip,
...                             subcategory.text.split('\n'))):
...         print "entry output: (%s)" % entry
...     print "subcategory output (%s)" % subcategory.tag
... 
entry output: (entry1)
entry output: (entry2)
subcategory output (subcategory)
>>> 

I'm not really convinced this is a python question, despite the tags and penultimate sentence in the question, but here's a python answer.

>>> from xml.etree import ElementTree as etree
>>> corpus = '''<category1>
...       <subcategory>
...         entry1
...         entry2
...       </subcategory>
... </category1>
... '''
>>> doc = etree.fromstring(corpus)
>>> for subcategory in doc.getchildren():
...     for entry in filter(bool,
...                         map(str.strip,
...                             subcategory.text.split('\n'))):
...         print "entry output: (%s)" % entry
...     print "subcategory output (%s)" % subcategory.tag
... 
entry output: (entry1)
entry output: (entry2)
subcategory output (subcategory)
>>> 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文