嵌套元素和脚本

发布于 2024-09-29 18:59:02 字数 739 浏览 3 评论 0原文

我目前正在尝试创建和应用程序(在 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 技术交流群。

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

发布评论

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

评论(1

月牙弯弯 2024-10-06 18:59:02

在大多数情况下,您可以按照 XML 中表示的方式来表示数据:

public class Conversation 
{
    public IEnumerable<Message> Messages {get;set;}
}

public class Message
{
    public string From {get;set;}
    public IEnumerable<TypeWriter> TypeWriters {get;set;}
}

...等。但是,如果您的 XML 将允许以任何顺序使用不同的节点类型(例如,打字机和计算机可互换),则您需要调整这个。

当涉及到文本节点时,文字文本和其他节点都应被视为一种操作。

public class TypewriterText
{
    public IEnumerable<ITypewriterTextAction> TextActions {get;set;}
}

public enum TypeWriterTextActionType
{
    Plain,
    Replace
}

public interface ITypewriterTextAction
{
    TypeWriterTextActionType ActionType {get;}
}

public class PlainTypeWriterTextAction : ITypewriterTextAction
{
    public TypeWriterTextActionType ActionType 
    {
        get {return TypeWriterTextActionType.Plain;
    }
    public string TextToWrite {get;set;}
}

public class ReplaceTypeWriterTextAction : ITypewriterTextAction
{
    public TypeWriterTextActionType ActionType 
    {
        get {return TypeWriterTextActionType.Replace;
    }
    public string OriginalText {get;set;}
    public string ReplacementText {get;set;}
}

使用 LINQ to XML 等技术将 XML 解析为这些对象,然后编写可以获取这些对象并执行适当操作的方法。例如,您需要一个知道如何执行普通动画的类,另一个知道如何执行替换动画的类,并且您可以在每个操作的 Type 属性上使用 switch 语句来确定要使用哪个动画类。

For the most part, you can represent your data the way it's represented in the XML:

public class Conversation 
{
    public IEnumerable<Message> Messages {get;set;}
}

public class Message
{
    public string From {get;set;}
    public IEnumerable<TypeWriter> TypeWriters {get;set;}
}

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

public class TypewriterText
{
    public IEnumerable<ITypewriterTextAction> TextActions {get;set;}
}

public enum TypeWriterTextActionType
{
    Plain,
    Replace
}

public interface ITypewriterTextAction
{
    TypeWriterTextActionType ActionType {get;}
}

public class PlainTypeWriterTextAction : ITypewriterTextAction
{
    public TypeWriterTextActionType ActionType 
    {
        get {return TypeWriterTextActionType.Plain;
    }
    public string TextToWrite {get;set;}
}

public class ReplaceTypeWriterTextAction : ITypewriterTextAction
{
    public TypeWriterTextActionType ActionType 
    {
        get {return TypeWriterTextActionType.Replace;
    }
    public string OriginalText {get;set;}
    public string ReplacementText {get;set;}
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文