嵌套元素和脚本
我目前正在尝试创建和应用程序(在 c# 中)解析 XML 文件,并根据文本中的元素和标签更改文本。
示例:
<conversation>
<message from=Bob>
<typewriter dif=0.5>
<Text>
Bob: Hello <replace>Country<with>World</with></replace>`
</Text>
</typewriter>
<message>
</conversation>
输出如下所示:
它开始像旧打字机一样写入“Bob:Hello Country”(字母对字母),当写入“Country”时,它将删除该单词并开始写入 World。所以最终的输出将是“Bob:Hello World”
所以这是我的问题: 解析 XML 文件后,存储数据以便程序知道哪些元素包含哪些元素的好方法是什么? (例如,消息包含打字机)
让程序识别文本元素内的脚本标签。我该怎么做?以及如何让它像示例一样工作?
我在这里并不要求完整的代码,只是要求一些正确方向的指针。我还是一个编程初学者,所以我想学习。
我不知道要搜索什么,所以如果类似的内容已经发布,那么我很抱歉。
I'am currently trying to create and application (In c#) that parse a XML file and from that change the Text depending on the elements and tags inside the text.
Example:
<conversation>
<message from=Bob>
<typewriter dif=0.5>
<Text>
Bob: Hello <replace>Country<with>World</with></replace>`
</Text>
</typewriter>
<message>
</conversation>
The output would look like this:
It starts writing "Bob: Hello Country" like a old typewriter (letter for letter) and when "Country" is written it will remove that word and start to write World instead. So the final output would be "Bob: Hello World"
So here are my questions:
After parsing the XML file, what is a good approach for storing the data so the program knows what elements contains what elements? (eg. Message contains typewriter)
To get the program to recognize script tags inside of the Text element. How do i do that? and how get it to work like the example?
I'am not asking for completed code here, just some pointers in the right direction. I'am still a beginner at programming so i want to learn.
I didn't know what to search for so if something like this is already posted then i'am sorry.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在大多数情况下,您可以按照 XML 中表示的方式来表示数据:
...等。但是,如果您的 XML 将允许以任何顺序使用不同的节点类型(例如,打字机和计算机可互换),则您需要调整这个。
当涉及到文本节点时,文字文本和其他节点都应被视为一种操作。
使用 LINQ to XML 等技术将 XML 解析为这些对象,然后编写可以获取这些对象并执行适当操作的方法。例如,您需要一个知道如何执行普通动画的类,另一个知道如何执行替换动画的类,并且您可以在每个操作的 Type 属性上使用 switch 语句来确定要使用哪个动画类。
For the most part, you can represent your data the way it's represented in the XML:
... etc. But if you XML is going to allow different node types in any order (e.g. typewriters and computers interchangebly), you'll need to tweak this.
When it comes to your Text node, the literal text and the other nodes should each be considered to be a kind of action.
Use a technology like LINQ to XML to parse the XML into these objects, then write methods that can take these objects and perform the appropriate actions. For example, you'll want one class that knows how to do the Plain animation and another that can do the Replace animation, and you can use a switch statement on each action's Type property to determine which animation class to use.