views:

49

answers:

1

Hello,

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.

A: 

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.

StriplingWarrior
Thx for the help, i will try it out. Probably will be back here soon :)
Andreas