Tech Off Thread

10 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

C# and XML - how to achieve this?

Back to Forum: Tech Off
  • User profile image
    chuawenching

    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

  • User profile image
    lorad

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


    Why not:

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

    then you can load the execute pieces and find the parameters for each function? (Or some other decrative way to refer back to the settings as parameters.......)

    I would expand upon this a little

    <software>
      <settings>
        <config>auto</config>
        <volume>80%</volume>
        <speed>10</speed>
      </settings>
      <command>
        <execute class=ClassName1 assembly=Assembly>Func1(%config%)</execute>
        <execute class=ClassName2>Func2(%volume%)</execute>
        <execute class=ClassName3>Func3(%speed%)</execute>
      </command>
    </software>

    If you are going to go to the trouble of letting them put things into the XML might as well allow for items from other assemblies etc.

  • User profile image
    Rossj

    lorad wrote:
    I would expand upon this a little

    <software>
      <settings>
        <config>auto</config>
        <volume>80%</volume>
        <speed>10</speed>
      </settings>
      <command>
        <execute class=ClassName1 assembly=Assembly>Func1(%config%)</execute>
        <execute class=ClassName2>Func2(%volume%)</execute>
        <execute class=ClassName3>Func3(%speed%)</execute>
      </command>
    </software>


    Here's a thought. Instead of using %config% why not use %/software/settings/config% and that way you can use it as the parameter to doc.SelectSingleNode() ? You might save yourself a step or two.

  • User profile image
    Sk4rlath

    You know, I'm doing something similar myself. If you  bug me enough, I might just zip up and post my XML-to-object graph code, though I make to guarantees about its security, performance, or bugs.

    Given something like
    <Form>
       <?using System.Windows.Forms?>
       <Controls>
          <Button Text="Click Me">
             <Size>75, 20</Size>
             <Location>8, 8</Location>
          </Button>
       </Controls>
    </Form>

    It will give you what you expect. Just pass in an XmlNode and you get an object back.

    To do the scripting bit, you can probably just make a set of scripting objects and have all the text parsing done there.

  • User profile image
    chuawenching

    Oh wow.. can do that?

    (%config%)

    I see...

    Hi Sk3rlath, your codes look similar to wfml .. windows form markup language.. released in windowsform.net (i think so???)

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

    Thanks.

  • User profile image
    blazko

    I am not 100% sure what you are trying to achieve. Are you trying to de-serialize an XML document straight into a C# object? If so, Look at the System.Xml.XmlSerializer class, it can do what you want but you will have to change the format of your XML or transform your XML first into something the XmlSerializer can understand.

    If you are just trying to parse an XML document however to load some config settings you should probably start looking in the System.Xml namespace particularly System.Xml.XmlDocument.

  • User profile image
    blazko

    Upon further reading of your post, it seems like you may be trying to do something similar to what the NAnt build tool does? (http://nant.sourceforge.net) It essentially executes a build script written in XML.

    In that case, I don't believe my advice helps you very much, and I would suggest looking at some of the NAnt source code.

  • User profile image
    chuawenching

    Thanks for the reply.

    I had looked into wfml parser.. i think that the parser is a good start for me.

    Cheers.

  • User profile image
    chuawenching

    lorad wrote:

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


    Why not:

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

    then you can load the execute pieces and find the parameters for each function? (Or some other decrative way to refer back to the settings as parameters.......)

    I would expand upon this a little

    <software>
      <settings>
        <config>auto</config>
        <volume>80%</volume>
        <speed>10</speed>
      </settings>
      <command>
        <execute class=ClassName1 assembly=Assembly>Func1(%config%)</execute>
        <execute class=ClassName2>Func2(%volume%)</execute>
        <execute class=ClassName3>Func3(%speed%)</execute>
      </command>
    </software>

    If you are going to go to the trouble of letting them put things into the XML might as well allow for items from other assemblies etc.



    Hi there,

    You suggested me to use (%Config%) right?

    1. Now how do i reference those values or parse those values in C# or .NET? Any help please? Any sample codes or snippets will do.

    2. Just curious what if i want to do this.

    <Device id="A1">
      <Name>Titan 12</Name>
      <Speed>10</Speed>
    </Device>
    <Device id="B3">
      <Name>Omeage T</Name>
      <Speed>130</Speed>
    </Device>

    <Command id="1">
      <!-- how do i specify which device id attribute either A1 or B3 ??? -->
      <Process>(%Device%)</Process>
    </Command>
    <Command id="2">
    ....
    </Command>

    Any help please?

    Thanks. Cheers.

  • User profile image
    Sven Groot

    Why not use XPath?
    It'd become something like this:
    /Sofware/Settings/Device[id='A1']

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.