C# and XML - some doubts to achieve this...

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi there,

I have some questions to ask... just say i have this xml file:

Scenario :-

Script.xml
========
<software>
<settings>
<config>auto</config>
<volume>80%</volume>
<speed>10</speed>
</settings>
<command>
<execute>Func1(auto)</execute>
<execute>Func2(80%)</execute>
<execute>Func3(10)</execute>
</command>
</software>

What i want to do is similar to a scripting?

1. I will load all the configuration within the <settings/> tag.

2. I then go into commands, and still will reference back the <settings/> tag.

maybe (guessing)

<settings>
<volume ref="#1">80%</volume>
</settings>
<command>
<execute>Func2(#1)</execute>
</command>

Execute this first :-

(behind the scene call) Func1(auto); // auto is get from the <settings/> tag.

(behind the scene call) Func2(80%); // 80% is get from the <settings/> tag.

(behind the scene call) Func3(10); // 10 is get from the <settings/> tag

It is something like C#, where the config, volume and speed are global
variables.

It will then be used inside each command, if necessary.

Propose Solution :-

xml script (which anyone can write with a text editor, as long follow the
right format - maybe schemas provided)

xml parser (validate the xml based on the schemas)

load the xml and execute it behind the scene

Any idea how this can be done? I know how to do this in C# script instead of
Xml Script?

Hope someone share shed some light for me. Thanks.
 
Hi Chua
Why don't you use an application configuration file for your application
"app.config" and the dynamic properites instead of creating your own.
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
You can't do this in the XML alone.

As you said, you will have to do some processing in the C# code you use to
read in the XML.

Chris.
 
How about something like below. Should give you the basic idea unless I
miss your need. Cheers!

private void button4_Click(object sender, System.EventArgs e)
{
// Serialize
Script s1 = new Script();
s1.Volume = .50m;
s1.Config = "abc";
s1.Speed = 100;
s1.Commands = new string[]{"one","two"};
string s1xml = s1.ToXmlString();

// Deserialize.
Script s2 = Script.FromXmlString(s1xml);

// Run Commands.
Script.TestRunScript(s2);
}

using System;
using System.Xml.Serialization;
using System.IO;

namespace MyApp
{
/// <summary>
/// Summary description for Script.
/// </summary>
public class Script
{
public string Config;
public decimal Volume;
public int Speed;
public string[] Commands;

public Script()
{
Commands = new string[0];
}

public string ToXmlString()
{
string xmlString;
XmlSerializer ser = new XmlSerializer(typeof(Script));
using(StringWriter sw = new StringWriter())
{
ser.Serialize(sw, this);
sw.Flush();
xmlString = sw.ToString();
}
return xmlString;
}

public static Script FromXmlString(string xmlString)
{
Script script = null;
XmlSerializer ser = new XmlSerializer(typeof(Script));
using (StringReader sr = new StringReader(xmlString))
{
script = (Script)ser.Deserialize(sr);
}
return script;
}

public static void TestRunScript(Script script)
{
Console.WriteLine(""+script.Speed);
Console.WriteLine(""+script.Volume);

foreach(string s in script.Commands)
{
Console.WriteLine("Run Command:"+s);
}
}
}
}
 
Hi William,

Thanks for the reply. It sounds brilliant to me.

Do you have any demo with source code which i can download? i just want to
see how the whole thing works. Hope can be a good start for me.

Thanks :)

William Stacey said:
How about something like below. Should give you the basic idea unless I
miss your need. Cheers!

private void button4_Click(object sender, System.EventArgs e)
{
// Serialize
Script s1 = new Script();
s1.Volume = .50m;
s1.Config = "abc";
s1.Speed = 100;
s1.Commands = new string[]{"one","two"};
string s1xml = s1.ToXmlString();

// Deserialize.
Script s2 = Script.FromXmlString(s1xml);

// Run Commands.
Script.TestRunScript(s2);
}

using System;
using System.Xml.Serialization;
using System.IO;

namespace MyApp
{
/// <summary>
/// Summary description for Script.
/// </summary>
public class Script
{
public string Config;
public decimal Volume;
public int Speed;
public string[] Commands;

public Script()
{
Commands = new string[0];
}

public string ToXmlString()
{
string xmlString;
XmlSerializer ser = new XmlSerializer(typeof(Script));
using(StringWriter sw = new StringWriter())
{
ser.Serialize(sw, this);
sw.Flush();
xmlString = sw.ToString();
}
return xmlString;
}

public static Script FromXmlString(string xmlString)
{
Script script = null;
XmlSerializer ser = new XmlSerializer(typeof(Script));
using (StringReader sr = new StringReader(xmlString))
{
script = (Script)ser.Deserialize(sr);
}
return script;
}

public static void TestRunScript(Script script)
{
Console.WriteLine(""+script.Speed);
Console.WriteLine(""+script.Volume);

foreach(string s in script.Commands)
{
Console.WriteLine("Run Command:"+s);
}
}
}
}
 
?? The source I included in the post is what I did for you. Do you have
specific questions?

--
William Stacey, MVP
http://mvp.support.microsoft.com

Chua Wen Ching said:
Hi William,

Thanks for the reply. It sounds brilliant to me.

Do you have any demo with source code which i can download? i just want to
see how the whole thing works. Hope can be a good start for me.

Thanks :)

William Stacey said:
How about something like below. Should give you the basic idea unless I
miss your need. Cheers!

private void button4_Click(object sender, System.EventArgs e)
{
// Serialize
Script s1 = new Script();
s1.Volume = .50m;
s1.Config = "abc";
s1.Speed = 100;
s1.Commands = new string[]{"one","two"};
string s1xml = s1.ToXmlString();

// Deserialize.
Script s2 = Script.FromXmlString(s1xml);

// Run Commands.
Script.TestRunScript(s2);
}

using System;
using System.Xml.Serialization;
using System.IO;

namespace MyApp
{
/// <summary>
/// Summary description for Script.
/// </summary>
public class Script
{
public string Config;
public decimal Volume;
public int Speed;
public string[] Commands;

public Script()
{
Commands = new string[0];
}

public string ToXmlString()
{
string xmlString;
XmlSerializer ser = new XmlSerializer(typeof(Script));
using(StringWriter sw = new StringWriter())
{
ser.Serialize(sw, this);
sw.Flush();
xmlString = sw.ToString();
}
return xmlString;
}

public static Script FromXmlString(string xmlString)
{
Script script = null;
XmlSerializer ser = new XmlSerializer(typeof(Script));
using (StringReader sr = new StringReader(xmlString))
{
script = (Script)ser.Deserialize(sr);
}
return script;
}

public static void TestRunScript(Script script)
{
Console.WriteLine(""+script.Speed);
Console.WriteLine(""+script.Volume);

foreach(string s in script.Commands)
{
Console.WriteLine("Run Command:"+s);
}
}
}
}

--
William Stacey, MVP
http://mvp.support.microsoft.com

Chua Wen Ching said:
Hi there,

I have some questions to ask... just say i have this xml file:

Scenario :-

Script.xml
========
<software>
<settings>
<config>auto</config>
<volume>80%</volume>
<speed>10</speed>
</settings>
<command>
<execute>Func1(auto)</execute>
<execute>Func2(80%)</execute>
<execute>Func3(10)</execute>
</command>
</software>

What i want to do is similar to a scripting?

1. I will load all the configuration within the <settings/> tag.

2. I then go into commands, and still will reference back the
 
Hi William,

I have couple of doubts to ask you.

A) I think the flow of the scripting app should like this right?

Ok, i assume this is what i can do for scripting.

1. Load the xml file.

2. Validate the xml file for syntax and based on the proper schemas.

3. Deserialize the xml file on the fly into cs code(but how can i do that, i
know how to do manually with xsd.exe tool ??? ).

4. Compile the c# code on the fly with CodeDom.Compiler.

Then execute the functions.

Am i right? I felt a bit weird with my procedures...

B) Your script class, is it get after you deserialize the XML on the fly ???
to double check

C) I also heard that in the xml, you can do this:

<settings>
<volume>80%</volume>
</settings>
<command>
<execute>func1((%volume%))</execute>
</command>

Have you tried this way before?

Thanks.

Thanks.

William Stacey said:
?? The source I included in the post is what I did for you. Do you have
specific questions?

--
William Stacey, MVP
http://mvp.support.microsoft.com

Chua Wen Ching said:
Hi William,

Thanks for the reply. It sounds brilliant to me.

Do you have any demo with source code which i can download? i just want to
see how the whole thing works. Hope can be a good start for me.

Thanks :)

William Stacey said:
How about something like below. Should give you the basic idea unless I
miss your need. Cheers!

private void button4_Click(object sender, System.EventArgs e)
{
// Serialize
Script s1 = new Script();
s1.Volume = .50m;
s1.Config = "abc";
s1.Speed = 100;
s1.Commands = new string[]{"one","two"};
string s1xml = s1.ToXmlString();

// Deserialize.
Script s2 = Script.FromXmlString(s1xml);

// Run Commands.
Script.TestRunScript(s2);
}

using System;
using System.Xml.Serialization;
using System.IO;

namespace MyApp
{
/// <summary>
/// Summary description for Script.
/// </summary>
public class Script
{
public string Config;
public decimal Volume;
public int Speed;
public string[] Commands;

public Script()
{
Commands = new string[0];
}

public string ToXmlString()
{
string xmlString;
XmlSerializer ser = new XmlSerializer(typeof(Script));
using(StringWriter sw = new StringWriter())
{
ser.Serialize(sw, this);
sw.Flush();
xmlString = sw.ToString();
}
return xmlString;
}

public static Script FromXmlString(string xmlString)
{
Script script = null;
XmlSerializer ser = new XmlSerializer(typeof(Script));
using (StringReader sr = new StringReader(xmlString))
{
script = (Script)ser.Deserialize(sr);
}
return script;
}

public static void TestRunScript(Script script)
{
Console.WriteLine(""+script.Speed);
Console.WriteLine(""+script.Volume);

foreach(string s in script.Commands)
{
Console.WriteLine("Run Command:"+s);
}
}
}
}

--
William Stacey, MVP
http://mvp.support.microsoft.com

Hi there,

I have some questions to ask... just say i have this xml file:

Scenario :-

Script.xml
========
<software>
<settings>
<config>auto</config>
<volume>80%</volume>
<speed>10</speed>
</settings>
<command>
<execute>Func1(auto)</execute>
<execute>Func2(80%)</execute>
<execute>Func3(10)</execute>
</command>
</software>

What i want to do is similar to a scripting?

1. I will load all the configuration within the <settings/> tag.

2. I then go into commands, and still will reference back the
tag.

maybe (guessing)

<settings>
<volume ref="#1">80%</volume>
</settings>
<command>
<execute>Func2(#1)</execute>
</command>

Execute this first :-

(behind the scene call) Func1(auto); // auto is get from the
tag.

(behind the scene call) Func2(80%); // 80% is get from the <settings/>
tag.

(behind the scene call) Func3(10); // 10 is get from the <settings/> tag

It is something like C#, where the config, volume and speed are global
variables.

It will then be used inside each command, if necessary.

Propose Solution :-

xml script (which anyone can write with a text editor, as long follow the
right format - maybe schemas provided)

xml parser (validate the xml based on the schemas)

load the xml and execute it behind the scene

Any idea how this can be done? I know how to do this in C# script instead
of
Xml Script?

Hope someone share shed some light for me. Thanks.
 
Back
Top