Reading / Writing XML's - noob question

L

Lang Murphy

I'm baaaaack... some of you answered a question I had last week. Only
problem is: I'm a dope who doesn't understand most of what y'all posted. Raw
noob when it comes to .Net and C#.

So I'm going to be more specific... All I need to do at the moment is figure
out how to replace our usage of .ini files as configuration files for our
small, utility type apps. I'm not looking to use XML as a data stream or
anything like that. Not right now, anyway...

So... let's say I've got this ini file:

[OS]
OS1 = "Windows 2000"
OS2 = "Windows XP"
OS3 = "Windows Vista"

[RAM]
RAM1 = "256MB"
RAM2 = "512MB"
RAM3 = "1GB"

[Model]
Mod1 = "D620"
Mod2 = ""
Mod3 = ""

Which I -think- translates to something like this in XML:

<?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <section name="OS">
<setting name="OS1" value=""Windows 2000"" />
<setting name="OS2" value=""Windows XP"" />
<setting name="OS3" value=""Windows Vista"" />
</section>
- <section name="RAM">
<setting name="RAM1" value=""256MB"" />
<setting name="RAM2" value=""512MB"" />
<setting name="RAM3" value=""1GB"" />
</section>
- <section name="Model">
<setting name="Mod1" value=""D620"" />
<setting name="Mod2" value="""" />
<setting name="Mod3" value="""" />
</section>
</configuration>

All I want to know is: 1. How to read, say, OS2 out of the OS section. 2.
How to insert a RAM4 setting name with the value of "2GB" 3. How to modify
the value of Mod2. I suppose all that goes out the window if the structure
of the XML file in incorrect...

At any rate, all responses greatly appreciated in advance!

Thanks,

Lang
 
N

Nicholas Paldino [.NET/C# MVP]

Lang,

There is no definitive way to translate an INI file to an XML file.

Why not just use the same INI files and then call the INI file functions
through the P/Invoke layer?
 
L

Lang Murphy

Nicholas Paldino said:
Lang,

There is no definitive way to translate an INI file to an XML file.

Why not just use the same INI files and then call the INI file
functions through the P/Invoke layer?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lang Murphy said:
I'm baaaaack... some of you answered a question I had last week. Only
problem is: I'm a dope who doesn't understand most of what y'all posted.
Raw noob when it comes to .Net and C#.

So I'm going to be more specific... All I need to do at the moment is
figure out how to replace our usage of .ini files as configuration files
for our small, utility type apps. I'm not looking to use XML as a data
stream or anything like that. Not right now, anyway...

So... let's say I've got this ini file:

[OS]
OS1 = "Windows 2000"
OS2 = "Windows XP"
OS3 = "Windows Vista"

[RAM]
RAM1 = "256MB"
RAM2 = "512MB"
RAM3 = "1GB"

[Model]
Mod1 = "D620"
Mod2 = ""
Mod3 = ""

Which I -think- translates to something like this in XML:

<?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <section name="OS">
<setting name="OS1" value=""Windows 2000"" />
<setting name="OS2" value=""Windows XP"" />
<setting name="OS3" value=""Windows Vista"" />
</section>
- <section name="RAM">
<setting name="RAM1" value=""256MB"" />
<setting name="RAM2" value=""512MB"" />
<setting name="RAM3" value=""1GB"" />
</section>
- <section name="Model">
<setting name="Mod1" value=""D620"" />
<setting name="Mod2" value="""" />
<setting name="Mod3" value="""" />
</section>
</configuration>

All I want to know is: 1. How to read, say, OS2 out of the OS section. 2.
How to insert a RAM4 setting name with the value of "2GB" 3. How to
modify the value of Mod2. I suppose all that goes out the window if the
structure of the XML file in incorrect...

At any rate, all responses greatly appreciated in advance!

Thanks,

Lang


OK... are you saying the XML example here is not well formed or valid? I
mean... LOL, using the P/invoke layer is a bit beyond my comprehension at
this point in time.

And... thanks for the response!

Lang
 
C

Chris Dunaway

I'm baaaaack... some of you answered a question I had last week. Only
problem is: I'm a dope who doesn't understand most of what y'all posted. Raw
noob when it comes to .Net and C#.

So I'm going to be more specific... All I need to do at the moment is figure
out how to replace our usage of .ini files as configuration files for our
small, utility type apps. I'm not looking to use XML as a data stream or
anything like that. Not right now, anyway...

So... let's say I've got this ini file:

[OS]
OS1 = "Windows 2000"
OS2 = "Windows XP"
OS3 = "Windows Vista"

[RAM]
RAM1 = "256MB"
RAM2 = "512MB"
RAM3 = "1GB"

[Model]
Mod1 = "D620"
Mod2 = ""
Mod3 = ""

Which I -think- translates to something like this in XML:

<?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <section name="OS">
<setting name="OS1" value=""Windows 2000"" />
<setting name="OS2" value=""Windows XP"" />
<setting name="OS3" value=""Windows Vista"" />
</section>
- <section name="RAM">
<setting name="RAM1" value=""256MB"" />
<setting name="RAM2" value=""512MB"" />
<setting name="RAM3" value=""1GB"" />
</section>
- <section name="Model">
<setting name="Mod1" value=""D620"" />
<setting name="Mod2" value="""" />
<setting name="Mod3" value="""" />
</section>
</configuration>

All I want to know is: 1. How to read, say, OS2 out of the OS section. 2.
How to insert a RAM4 setting name with the value of "2GB" 3. How to modify
the value of Mod2. I suppose all that goes out the window if the structure
of the XML file in incorrect...

At any rate, all responses greatly appreciated in advance!

Thanks,

Lang

Look up XPath queries on Google or visit this site which has some good
lessons:

http://www.w3schools.com/

Basically, you would use the SelectSingleNode method and pass in an
Xpath string to the node that you want. Here's an off the top of my
head example (watch for typos):

XmlNode node = rootnode.SelectSingleNode("/configuration/
section[@name='OS']/setting[@name='OS2']");
Console.WriteLine(node.Text);

So this command would select the setting node with name 'OS2' that
exists under the section node with name 'OS' that exists under the
configuration node.

If the node cannot be found, the result will be null so it's best to
check if the result is null before trying to get the value of the
node. Keep in mind that xml is case sensitive, so if you get a null
result and you know the node exists in the xml, double check your
xpath.

Hope this gives you some ideas.

Chris
 
N

Nicholas Paldino [.NET/C# MVP]

Lang,

No, I'm not saying it is invalid, but rather, there is no way of saying
"these elements in the INI file correspond to these elements in the XML
file" (at least, there is no standard way that I know of). I could just as
easily represent the data in a different way.

Actually, looking at it now, the XML is not valid or well formed, but
I'm nitpicking, since you copied and pasted it from IE, it seems (and the
expand/collapse tags are there, as well as two sets of double quotes around
strings).

I would look at http://www.pinvoke.net and enter in the API functions
from whatever previous application was using the INI files. It will most
definitely return C# declarations you can use to make the same calls in your
C# app.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Lang Murphy said:
Nicholas Paldino said:
Lang,

There is no definitive way to translate an INI file to an XML file.

Why not just use the same INI files and then call the INI file
functions through the P/Invoke layer?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lang Murphy said:
I'm baaaaack... some of you answered a question I had last week. Only
problem is: I'm a dope who doesn't understand most of what y'all posted.
Raw noob when it comes to .Net and C#.

So I'm going to be more specific... All I need to do at the moment is
figure out how to replace our usage of .ini files as configuration files
for our small, utility type apps. I'm not looking to use XML as a data
stream or anything like that. Not right now, anyway...

So... let's say I've got this ini file:

[OS]
OS1 = "Windows 2000"
OS2 = "Windows XP"
OS3 = "Windows Vista"

[RAM]
RAM1 = "256MB"
RAM2 = "512MB"
RAM3 = "1GB"

[Model]
Mod1 = "D620"
Mod2 = ""
Mod3 = ""

Which I -think- translates to something like this in XML:

<?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <section name="OS">
<setting name="OS1" value=""Windows 2000"" />
<setting name="OS2" value=""Windows XP"" />
<setting name="OS3" value=""Windows Vista"" />
</section>
- <section name="RAM">
<setting name="RAM1" value=""256MB"" />
<setting name="RAM2" value=""512MB"" />
<setting name="RAM3" value=""1GB"" />
</section>
- <section name="Model">
<setting name="Mod1" value=""D620"" />
<setting name="Mod2" value="""" />
<setting name="Mod3" value="""" />
</section>
</configuration>

All I want to know is: 1. How to read, say, OS2 out of the OS section.
2. How to insert a RAM4 setting name with the value of "2GB" 3. How to
modify the value of Mod2. I suppose all that goes out the window if the
structure of the XML file in incorrect...

At any rate, all responses greatly appreciated in advance!

Thanks,

Lang


OK... are you saying the XML example here is not well formed or valid? I
mean... LOL, using the P/invoke layer is a bit beyond my comprehension at
this point in time.

And... thanks for the response!

Lang
 
L

Lang Murphy

Nicholas Paldino said:
Lang,

No, I'm not saying it is invalid, but rather, there is no way of saying
"these elements in the INI file correspond to these elements in the XML
file" (at least, there is no standard way that I know of). I could just
as easily represent the data in a different way.

Actually, looking at it now, the XML is not valid or well formed, but
I'm nitpicking, since you copied and pasted it from IE, it seems (and the
expand/collapse tags are there, as well as two sets of double quotes
around strings).

I would look at http://www.pinvoke.net and enter in the API functions
from whatever previous application was using the INI files. It will most
definitely return C# declarations you can use to make the same calls in
your C# app.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Lang Murphy said:
Nicholas Paldino said:
Lang,

There is no definitive way to translate an INI file to an XML file.

Why not just use the same INI files and then call the INI file
functions through the P/Invoke layer?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I'm baaaaack... some of you answered a question I had last week. Only
problem is: I'm a dope who doesn't understand most of what y'all
posted. Raw noob when it comes to .Net and C#.

So I'm going to be more specific... All I need to do at the moment is
figure out how to replace our usage of .ini files as configuration
files for our small, utility type apps. I'm not looking to use XML as a
data stream or anything like that. Not right now, anyway...

So... let's say I've got this ini file:

[OS]
OS1 = "Windows 2000"
OS2 = "Windows XP"
OS3 = "Windows Vista"

[RAM]
RAM1 = "256MB"
RAM2 = "512MB"
RAM3 = "1GB"

[Model]
Mod1 = "D620"
Mod2 = ""
Mod3 = ""

Which I -think- translates to something like this in XML:

<?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <section name="OS">
<setting name="OS1" value=""Windows 2000"" />
<setting name="OS2" value=""Windows XP"" />
<setting name="OS3" value=""Windows Vista"" />
</section>
- <section name="RAM">
<setting name="RAM1" value=""256MB"" />
<setting name="RAM2" value=""512MB"" />
<setting name="RAM3" value=""1GB"" />
</section>
- <section name="Model">
<setting name="Mod1" value=""D620"" />
<setting name="Mod2" value="""" />
<setting name="Mod3" value="""" />
</section>
</configuration>

All I want to know is: 1. How to read, say, OS2 out of the OS section.
2. How to insert a RAM4 setting name with the value of "2GB" 3. How to
modify the value of Mod2. I suppose all that goes out the window if the
structure of the XML file in incorrect...

At any rate, all responses greatly appreciated in advance!

Thanks,

Lang


OK... are you saying the XML example here is not well formed or valid? I
mean... LOL, using the P/invoke layer is a bit beyond my comprehension at
this point in time.

And... thanks for the response!

Lang


Thanks for the follow up... you're right... copied and pasted from IE. I
understand what you're saying about the format of the XML. Thanks for the
link... I'll check it out, for sure.

Lang
 
L

Lang Murphy

Chris Dunaway said:
I'm baaaaack... some of you answered a question I had last week. Only
problem is: I'm a dope who doesn't understand most of what y'all posted.
Raw
noob when it comes to .Net and C#.

So I'm going to be more specific... All I need to do at the moment is
figure
out how to replace our usage of .ini files as configuration files for our
small, utility type apps. I'm not looking to use XML as a data stream or
anything like that. Not right now, anyway...

So... let's say I've got this ini file:

[OS]
OS1 = "Windows 2000"
OS2 = "Windows XP"
OS3 = "Windows Vista"

[RAM]
RAM1 = "256MB"
RAM2 = "512MB"
RAM3 = "1GB"

[Model]
Mod1 = "D620"
Mod2 = ""
Mod3 = ""

Which I -think- translates to something like this in XML:

<?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <section name="OS">
<setting name="OS1" value=""Windows 2000"" />
<setting name="OS2" value=""Windows XP"" />
<setting name="OS3" value=""Windows Vista"" />
</section>
- <section name="RAM">
<setting name="RAM1" value=""256MB"" />
<setting name="RAM2" value=""512MB"" />
<setting name="RAM3" value=""1GB"" />
</section>
- <section name="Model">
<setting name="Mod1" value=""D620"" />
<setting name="Mod2" value="""" />
<setting name="Mod3" value="""" />
</section>
</configuration>

All I want to know is: 1. How to read, say, OS2 out of the OS section. 2.
How to insert a RAM4 setting name with the value of "2GB" 3. How to
modify
the value of Mod2. I suppose all that goes out the window if the
structure
of the XML file in incorrect...

At any rate, all responses greatly appreciated in advance!

Thanks,

Lang

Look up XPath queries on Google or visit this site which has some good
lessons:

http://www.w3schools.com/

Basically, you would use the SelectSingleNode method and pass in an
Xpath string to the node that you want. Here's an off the top of my
head example (watch for typos):

XmlNode node = rootnode.SelectSingleNode("/configuration/
section[@name='OS']/setting[@name='OS2']");
Console.WriteLine(node.Text);

So this command would select the setting node with name 'OS2' that
exists under the section node with name 'OS' that exists under the
configuration node.

If the node cannot be found, the result will be null so it's best to
check if the result is null before trying to get the value of the
node. Keep in mind that xml is case sensitive, so if you get a null
result and you know the node exists in the xml, double check your
xpath.

Hope this gives you some ideas.

Chris


Chris,

Thanks for the response. I'll check out your example code and thanks for the
link; I'll check it out.

Lang
 
S

sloan

THis might give you some ideas:
http://www.codeproject.com/csharp/ReadWriteXmlIni.asp


For your specific question, there are xpath statements;
- <configuration>
- <section name="OS">
<setting name="OS1" value=""Windows 2000"" />
<setting name="OS2" value=""Windows XP"" />
<setting name="OS3" value=""Windows Vista"" />
</section>
- <section name="RAM">
<setting name="RAM1" value=""256MB"" />
<setting name="RAM2" value=""512MB"" />
<setting name="RAM3" value=""1GB"" />
</section>
- <section name="Model">
<setting name="Mod1" value=""D620"" />
<setting name="Mod2" value="""" />
<setting name="Mod3" value="""" />
</section>
</configuration>

If I wanted D620
//configuration/section[name='Model']/setting[name='Mod1'].Attributes['value
']
then you have to find the attribute
its something like that (going from memory.

Find the
..SelectNodes
..SelectSingleNode
methods in the XmlDocument and you'll see how to use
NodeList
objects.




Lang Murphy said:
I'm baaaaack... some of you answered a question I had last week. Only
problem is: I'm a dope who doesn't understand most of what y'all posted. Raw
noob when it comes to .Net and C#.

So I'm going to be more specific... All I need to do at the moment is figure
out how to replace our usage of .ini files as configuration files for our
small, utility type apps. I'm not looking to use XML as a data stream or
anything like that. Not right now, anyway...

So... let's say I've got this ini file:

[OS]
OS1 = "Windows 2000"
OS2 = "Windows XP"
OS3 = "Windows Vista"

[RAM]
RAM1 = "256MB"
RAM2 = "512MB"
RAM3 = "1GB"

[Model]
Mod1 = "D620"
Mod2 = ""
Mod3 = ""

Which I -think- translates to something like this in XML:

<?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <section name="OS">
<setting name="OS1" value=""Windows 2000"" />
<setting name="OS2" value=""Windows XP"" />
<setting name="OS3" value=""Windows Vista"" />
</section>
- <section name="RAM">
<setting name="RAM1" value=""256MB"" />
<setting name="RAM2" value=""512MB"" />
<setting name="RAM3" value=""1GB"" />
</section>
- <section name="Model">
<setting name="Mod1" value=""D620"" />
<setting name="Mod2" value="""" />
<setting name="Mod3" value="""" />
</section>
</configuration>

All I want to know is: 1. How to read, say, OS2 out of the OS section. 2.
How to insert a RAM4 setting name with the value of "2GB" 3. How to modify
the value of Mod2. I suppose all that goes out the window if the structure
of the XML file in incorrect...

At any rate, all responses greatly appreciated in advance!

Thanks,

Lang
 
L

Lang Murphy

sloan said:
THis might give you some ideas:
http://www.codeproject.com/csharp/ReadWriteXmlIni.asp


For your specific question, there are xpath statements;
- <configuration>
- <section name="OS">
<setting name="OS1" value=""Windows 2000"" />
<setting name="OS2" value=""Windows XP"" />
<setting name="OS3" value=""Windows Vista"" />
</section>
- <section name="RAM">
<setting name="RAM1" value=""256MB"" />
<setting name="RAM2" value=""512MB"" />
<setting name="RAM3" value=""1GB"" />
</section>
- <section name="Model">
<setting name="Mod1" value=""D620"" />
<setting name="Mod2" value="""" />
<setting name="Mod3" value="""" />
</section>
</configuration>

If I wanted D620
//configuration/section[name='Model']/setting[name='Mod1'].Attributes['value
']
then you have to find the attribute
its something like that (going from memory.

Find the
.SelectNodes
.SelectSingleNode
methods in the XmlDocument and you'll see how to use
NodeList
objects.




Lang Murphy said:
I'm baaaaack... some of you answered a question I had last week. Only
problem is: I'm a dope who doesn't understand most of what y'all posted. Raw
noob when it comes to .Net and C#.

So I'm going to be more specific... All I need to do at the moment is figure
out how to replace our usage of .ini files as configuration files for our
small, utility type apps. I'm not looking to use XML as a data stream or
anything like that. Not right now, anyway...

So... let's say I've got this ini file:

[OS]
OS1 = "Windows 2000"
OS2 = "Windows XP"
OS3 = "Windows Vista"

[RAM]
RAM1 = "256MB"
RAM2 = "512MB"
RAM3 = "1GB"

[Model]
Mod1 = "D620"
Mod2 = ""
Mod3 = ""

Which I -think- translates to something like this in XML:

<?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <section name="OS">
<setting name="OS1" value=""Windows 2000"" />
<setting name="OS2" value=""Windows XP"" />
<setting name="OS3" value=""Windows Vista"" />
</section>
- <section name="RAM">
<setting name="RAM1" value=""256MB"" />
<setting name="RAM2" value=""512MB"" />
<setting name="RAM3" value=""1GB"" />
</section>
- <section name="Model">
<setting name="Mod1" value=""D620"" />
<setting name="Mod2" value="""" />
<setting name="Mod3" value="""" />
</section>
</configuration>

All I want to know is: 1. How to read, say, OS2 out of the OS section. 2.
How to insert a RAM4 setting name with the value of "2GB" 3. How to
modify
the value of Mod2. I suppose all that goes out the window if the
structure
of the XML file in incorrect...

At any rate, all responses greatly appreciated in advance!

Thanks,

Lang


Thanks... I had found codeproject a few weeks ago and looked at this stuff,
but I'm still to new to understand everything that's going on. Thanks for
responding; appreciated!

Lang
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top