473,794 Members | 3,056 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com
Nov 16 '05 #1
6 1370
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

Nov 16 '05 #2
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.

"Chua Wen Ching" wrote:
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.
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com

Nov 16 '05 #3
How about something like below. Should give you the basic idea unless I
miss your need. Cheers!

private void button4_Click(o bject sender, System.EventArg s 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.FromXmlS tring(s1xml);

// Run Commands.
Script.TestRunS cript(s2);
}

using System;
using System.Xml.Seri alization;
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(t ypeof(Script));
using(StringWri ter sw = new StringWriter())
{
ser.Serialize(s w, this);
sw.Flush();
xmlString = sw.ToString();
}
return xmlString;
}

public static Script FromXmlString(s tring xmlString)
{
Script script = null;
XmlSerializer ser = new XmlSerializer(t ypeof(Script));
using (StringReader sr = new StringReader(xm lString))
{
script = (Script)ser.Des erialize(sr);
}
return script;
}

public static void TestRunScript(S cript script)
{
Console.WriteLi ne(""+script.Sp eed);
Console.WriteLi ne(""+script.Vo lume);

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

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

"Chua Wen Ching" <ch************ @nospam.hotmail .com> wrote in message
news:78******** *************** ***********@mic rosoft.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 <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.
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com


Nov 16 '05 #4
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 [MVP]" wrote:
How about something like below. Should give you the basic idea unless I
miss your need. Cheers!

private void button4_Click(o bject sender, System.EventArg s 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.FromXmlS tring(s1xml);

// Run Commands.
Script.TestRunS cript(s2);
}

using System;
using System.Xml.Seri alization;
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(t ypeof(Script));
using(StringWri ter sw = new StringWriter())
{
ser.Serialize(s w, this);
sw.Flush();
xmlString = sw.ToString();
}
return xmlString;
}

public static Script FromXmlString(s tring xmlString)
{
Script script = null;
XmlSerializer ser = new XmlSerializer(t ypeof(Script));
using (StringReader sr = new StringReader(xm lString))
{
script = (Script)ser.Des erialize(sr);
}
return script;
}

public static void TestRunScript(S cript script)
{
Console.WriteLi ne(""+script.Sp eed);
Console.WriteLi ne(""+script.Vo lume);

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

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

"Chua Wen Ching" <ch************ @nospam.hotmail .com> wrote in message
news:78******** *************** ***********@mic rosoft.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 <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.
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com


Nov 16 '05 #5
?? 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" <ch************ @nospam.hotmail .com> wrote in message
news:A7******** *************** ***********@mic rosoft.com...
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 [MVP]" wrote:
How about something like below. Should give you the basic idea unless I
miss your need. Cheers!

private void button4_Click(o bject sender, System.EventArg s 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.FromXmlS tring(s1xml);

// Run Commands.
Script.TestRunS cript(s2);
}

using System;
using System.Xml.Seri alization;
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(t ypeof(Script));
using(StringWri ter sw = new StringWriter())
{
ser.Serialize(s w, this);
sw.Flush();
xmlString = sw.ToString();
}
return xmlString;
}

public static Script FromXmlString(s tring xmlString)
{
Script script = null;
XmlSerializer ser = new XmlSerializer(t ypeof(Script));
using (StringReader sr = new StringReader(xm lString))
{
script = (Script)ser.Des erialize(sr);
}
return script;
}

public static void TestRunScript(S cript script)
{
Console.WriteLi ne(""+script.Sp eed);
Console.WriteLi ne(""+script.Vo lume);

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

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

"Chua Wen Ching" <ch************ @nospam.hotmail .com> wrote in message
news:78******** *************** ***********@mic rosoft.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 <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.
--
Regards,
Chua Wen Ching
Visit us at http://www.necoders.com



Nov 16 '05 #6
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.Compile r.

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 [MVP]" wrote:
?? 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" <ch************ @nospam.hotmail .com> wrote in message
news:A7******** *************** ***********@mic rosoft.com...
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 [MVP]" wrote:
How about something like below. Should give you the basic idea unless I
miss your need. Cheers!

private void button4_Click(o bject sender, System.EventArg s 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.FromXmlS tring(s1xml);

// Run Commands.
Script.TestRunS cript(s2);
}

using System;
using System.Xml.Seri alization;
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(t ypeof(Script));
using(StringWri ter sw = new StringWriter())
{
ser.Serialize(s w, this);
sw.Flush();
xmlString = sw.ToString();
}
return xmlString;
}

public static Script FromXmlString(s tring xmlString)
{
Script script = null;
XmlSerializer ser = new XmlSerializer(t ypeof(Script));
using (StringReader sr = new StringReader(xm lString))
{
script = (Script)ser.Des erialize(sr);
}
return script;
}

public static void TestRunScript(S cript script)
{
Console.WriteLi ne(""+script.Sp eed);
Console.WriteLi ne(""+script.Vo lume);

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

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

"Chua Wen Ching" <ch************ @nospam.hotmail .com> wrote in message
news:78******** *************** ***********@mic rosoft.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 <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.
> --
> Regards,
> Chua Wen Ching
> Visit us at http://www.necoders.com


Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1522
by: abbas reji | last post by:
--0-599929911-1059996886=:4358 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Content-Id: Content-Disposition: inline ________________________________________________________________________
2
1484
by: forvino | last post by:
Hi Geeks, Ihave a doubt in dotNet reflection. I m developing a tool which will returns set of public(access specifier) methods of the selected assembly. this works completely fine, when the selected components are
6
1582
by: ritesh | last post by:
Hi, I have been reading some text on C and C++ (i.e advanced books). One of the books mentioned that C++ requires a runtime support whereas C does not - what the author was trying to say was that once you compile a C program the executable created is all that is needed whereas if you compile a C++ program the executable created requires a C++ runtime installed on your system to run the program. Can someone please provide more...
2
1414
by: forvino | last post by:
Hi Geeks, Ihave a doubt in dotNet reflection. I m developing a tool which will returns set of public(access specifier) methods of the selected assembly. this works completely fine, when the selected components are
1
1189
by: NagaKiran | last post by:
Hi I want to post VBA related doubts. Where can I post my doubts in VBA? thanks bye
0
881
by: forvino | last post by:
Hi Geeks, Ihave a doubt in dotNet reflection. I m developing a tool which will returns set of public(access specifier) methods of the selected assembly. this works completely fine, when the selected components are
17
3060
by: lovecreatesbeauty | last post by:
1. In the following code, is the code (line 11) legal? Is there a notice in the document to tell callers that the parameter s1 should receive an array variable, i.e. type char, but not a variable of char *? p1 and p2 point to the same things but they must be declared as different types? Is it nature? char p1 = "hello123456"; char *p2 = "world"; strncpy(p1, p2, strlen(p2));
0
997
by: Saranya | last post by:
Hi friends I m Saranya.In my coding there r 2 tables 1.category 2.designation 1.contains category id,category name 2.category id,designation id,designation name in my interface there is a add button from where i added designation details with category id fetched from category master.
0
940
by: =?Utf-8?B?Sm9obg==?= | last post by:
Hi all, We have one product. It is client/server based application. We have developed client application in VC++ 6.0. Our client is desktop based application like dialog based application. Client application will connect to server according to user requests. This is current scenario. We would like to make this application as a web-based application. I have core functionality in VC++ application in client application. I can put this...
0
9672
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10213
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9037
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.