By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,053 Members | 922 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,053 IT Pros & Developers. It's quick & easy.

Reading / Writing XML's - noob question

P: n/a
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
Jun 26 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"Lang Murphy" <la*********@hotmail.comwrote in message
news:63**********************************@microsof t.com...
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


Jun 26 '07 #2

P: n/a
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:us**************@TK2MSFTNGP02.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com

"Lang Murphy" <la*********@hotmail.comwrote in message
news:63**********************************@microsof t.com...
>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

Jun 26 '07 #3

P: n/a
On Jun 26, 10:56 am, "Lang Murphy" <lang_mur...@hotmail.comwrote:
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

Jun 26 '07 #4

P: n/a
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]
- mv*@spam.guard.caspershouse.com
"Lang Murphy" <la*********@hotmail.comwrote in message
news:u5**************@TK2MSFTNGP04.phx.gbl...
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:us**************@TK2MSFTNGP02.phx.gbl...
>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]
- mv*@spam.guard.caspershouse.com

"Lang Murphy" <la*********@hotmail.comwrote in message
news:63**********************************@microso ft.com...
>>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

Jun 26 '07 #5

P: n/a
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2****************@TK2MSFTNGP03.phx.gbl...
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]
- mv*@spam.guard.caspershouse.com
"Lang Murphy" <la*********@hotmail.comwrote in message
news:u5**************@TK2MSFTNGP04.phx.gbl...
>"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:us**************@TK2MSFTNGP02.phx.gbl...
>>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]
- mv*@spam.guard.caspershouse.com

"Lang Murphy" <la*********@hotmail.comwrote in message
news:63**********************************@micros oft.com...
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

Jun 26 '07 #6

P: n/a
"Chris Dunaway" <du******@gmail.comwrote in message
news:11**********************@k79g2000hse.googlegr oups.com...
On Jun 26, 10:56 am, "Lang Murphy" <lang_mur...@hotmail.comwrote:
>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

Jun 26 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.