|
-
Mar 9th, 2002, 12:16 AM
#1
Thread Starter
Frenzied Member
XML Galore: Replacement for INI files
here are some functions that might come in handy
here is XML file before changes
Code:
<?xml version="1.0" standalone="no"?>
<ProgramSettings>
<Lingo>German</Lingo>
</ProgramSettings>
here are 2 lines of code, one to set a new value, and the other to read the value..
Code:
MessageBox.Show(myXML.setXMLNodeInXMLFile(Environment.CurrentDirectory + @"\settings.xml", "ProgramSettings", "Lingo", "Spanish"));
MessageBox.Show(myXML.getXMLNodeFromXMLFile(Environment.CurrentDirectory + @"\settings.xml", "ProgramSettings", "Lingo"));
here is the result
Code:
<?xml version="1.0" standalone="no"?>
<ProgramSettings>
<Lingo>Spanish</Lingo>
</ProgramSettings>
and here is the code
PHP Code:
//Descrtiption: All functions and methods related to XML
//File: myXML.cs
//Author Name: Kovan Abdulla
//Author Email: [email][email protected][/email]
//Author Homepage: [url]http://www.imetasoft.com[/url]
using System;
using System.Xml;
namespace CSharpDummyApp
{
public class myXML
{
private myXML()
{
}
public static string getXMLNodeFromXMLFile( string xmlFile,
string parentNode,
string node)
//PURPOSE: To retrieve the text between an xml tag from a xml file
//REQUIRED: the node or the text between xml tag and the parent of that node
//PROMOSE: return either file not found or the text of the given node
{
try
{
XmlDocument doc = new XmlDocument(); // get an XML document
doc.Load(xmlFile); //load the file
XmlNodeList xmlNode = doc.GetElementsByTagName(node);
for(int i = 0; i < xmlNode.Count; i++) //loop through all the found nodes
{
if (xmlNode[i].ParentNode.Name == parentNode) //find the one we want in the collection
{
return xmlNode[i].InnerText.ToString(); //return the text
}
}
return "Node belonging to " + parentNode + " was not found in " + xmlFile;
}
catch(System.Exception e) //catch the exception such as if the file passed in is not found
{
return e.Message;
}
}
public static string setXMLNodeInXMLFile( string xmlFile,
string parentNode,
string node,
string nodeValue)
//PURPOSE: To set the text between an xml tag in a xml file
//REQUIRED: the node or the text between xml tag and the parent of that node, and new text
//PROMOSE: return either file not found or success message
{
try
{
XmlDocument doc = new XmlDocument(); // get an XML document
doc.Load(xmlFile); //load the file
XmlNodeList xmlNode = doc.GetElementsByTagName(node);
for(int i = 0; i < xmlNode.Count; i++) //loop through all the found nodes
{
if (xmlNode[i].ParentNode.Name == parentNode) //find the one we want in the collection
{
xmlNode[i].InnerText = nodeValue; //set the text
doc.Save(xmlFile); //save the file back
return "Node " + node + "'s value has been changed to " + nodeValue + " succesfuly";
}
}
return "Node belonging to " + parentNode + " was not found in " + xmlFile;
}
catch(System.Exception e) //catch the exception such as if the file passed in is not found
{
return e.Message;
}
}
}
}
-
Mar 10th, 2002, 05:14 PM
#2
Addicted Member
excellent, thanks kovan!
-
Mar 10th, 2002, 05:36 PM
#3
Thread Starter
Frenzied Member
how is the coding style? comments? and so on
i will be posting a lot of stuff like this in the future...
so i would like people to undrestand it
hehe
-
Mar 11th, 2002, 10:10 AM
#4
excellent job kovan.
you da man!
-
Mar 18th, 2002, 05:02 AM
#5
Addicted Member
i managed to convert that code in to vb.net, and got it to work, although i'm having problems reading from an xml file that's structured like this:
Code:
<?xml version="1.0" standalone="no"?>
<Users>
<User1>
<Category1>
<Account1 Username="blah" Password="mypass1" />
<Account2 Username="abc123" Password="mypass2" />
</Category1>
<Category2>
<Account1 Username="dfgs" Password="dfgdrtgre" />
<Category2>
</User1>
<User2>
<Category1>
<Account1 Username="ertret" Password="cvbghgf" />
</Category1
</User2
</Users>
first of all, it would seem that you can't have two parent nodes with the same name, even if they have seperate parent nodes themselves. secondly, i haven't a clue how to read/write to that. can anyone help?
-
Mar 18th, 2002, 11:22 AM
#6
Thread Starter
Frenzied Member
actually i have that on my list
more functions will be written to deal with such structure
i just havent coded them yet,
next release will contain more functions and probably alternative ways of doing things
-
Mar 18th, 2002, 11:27 AM
#7
Addicted Member
*pats kovan on the back
keep up the good work man!
-
Mar 18th, 2002, 01:47 PM
#8
Hyperactive Member
Originally posted by donut
i managed to convert that code in to vb.net, and got it to work, although i'm having problems reading from an xml file that's structured like this:
Code:
<?xml version="1.0" standalone="no"?>
<Users>
<User1>
<Category1>
<Account1 Username="blah" Password="mypass1" />
<Account2 Username="abc123" Password="mypass2" />
</Category1>
<Category2>
<Account1 Username="dfgs" Password="dfgdrtgre" />
<Category2>
</User1>
<User2>
<Category1>
<Account1 Username="ertret" Password="cvbghgf" />
</Category1
</User2
</Users>
first of all, it would seem that you can't have two parent nodes with the same name, even if they have seperate parent nodes themselves. secondly, i haven't a clue how to read/write to that. can anyone help?
<?xml version="1.0" standalone="no"?>
<Users>
<User1>
<Category1>
<Account1 Username="blah" Password="mypass1" />
<Account2 Username="abc123" Password="mypass2" />
</Category1>
<Category2>
<Account1 Username="dfgs" Password="dfgdrtgre" />
<Category2>
</User1>
<User2>
<Category1>
<Account1 Username="ertret" Password="cvbghgf" />
</Category1>
</User2>
</Users>
-
Mar 18th, 2002, 04:57 PM
#9
Addicted Member
-
Mar 18th, 2002, 05:21 PM
#10
Lively Member
Kovan,
You should comment your code differently.
For example, the following method:
public static string getXMLNodeFromXMLFile(string xmlFile,
string parentNode,
string node)
Try to use:
/// <summary>
///
/// </summary>
/// <param name="xmlFile"></param>
/// <param name="parentNode"></param>
/// <param name="node"></param>
/// <returns></returns>
Not:
//PURPOSE: To retrieve the text between an xml tag from a xml file
//REQUIRED: the node or the text between xml tag and the parent of that node
//PROMOSE: return either file not found or the text of the given node
Typing 3 slashes (///) above a method or a class will generate the xml style comment. Based on these XML comments VS can generate XML / HTML comment files.
(Note: only works with C#)
Tip: if you take a look at gotdotnet there is a pretty cool (!) XSLT which generates the exact same help/documentation as VS it self based on your own XML inline comment.
-
Mar 19th, 2002, 08:21 AM
#11
Hyperactive Member
Basically, if i recall correctly, when you do the /// it makes documents based off the comments and such. Kinda sorta like javadoc that java creates (if your familure at all with java.) I wish VB.NET had this feature....
..::[ kleptos]::..
- Database Administrator (MSSQL 2000)
- Application Developer (C#)
- Web Developer (ASP.NET)

-
Mar 19th, 2002, 11:07 AM
#12
Hyperactive Member
excellent job kovan, i took your C# code and transformed it into VB.NET, which is easily doable, and it works like a charm. I have been looking for something similar to this, and this just hit the nail on the head... Thanks for all your effort and work on this.
..::[ kleptos]::..
- Database Administrator (MSSQL 2000)
- Application Developer (C#)
- Web Developer (ASP.NET)

-
Mar 19th, 2002, 11:46 PM
#13
Thread Starter
Frenzied Member
if you waited, i could have transformed it in to vb you know.. but you didnt want to wait.. hehe
actually could you send me your vb copy? i will put it inmy collection
oh and FYI, java sucks (and yes i programmed in it)
-
Mar 20th, 2002, 09:24 AM
#14
Hyperactive Member
I am VERY impatient when it comes to code, i would spend weeks converting something rather then waiting, i am wierd like that, besides, C# and VB.NET is close to the same, a {, ; ,} here and there and you've got it. I will send you over my VB code adaptation as soon as i fire up my laptop.
..::[ kleptos]::..
- Database Administrator (MSSQL 2000)
- Application Developer (C#)
- Web Developer (ASP.NET)

-
Mar 28th, 2002, 09:26 PM
#15
Junior Member
post the vb code?
could you post the vb version of this?
-
Mar 28th, 2002, 09:35 PM
#16
Member
What do I need to do to run this code? I created a new C# project and pasted in your code. And I created a XML file with..
Code:
<?xml version="1.0" standalone="no"?>
<ProgramSettings>
<Lingo>German</Lingo>
</ProgramSettings>
..which was saved in the root directory of my project. What else would I need to do to have it run? I'd like to play around with the source.
-
Mar 28th, 2002, 09:36 PM
#17
Member
Originally posted by kovan
oh and FYI, java sucks (and yes i programmed in it)
Any reason for that?
-
Mar 28th, 2002, 11:19 PM
#18
Thread Starter
Frenzied Member
Originally posted by AMDPwred
What do I need to do to run this code? I created a new C# project and pasted in your code. And I created a XML file with..
Code:
<?xml version="1.0" standalone="no"?>
<ProgramSettings>
<Lingo>German</Lingo>
</ProgramSettings>
..which was saved in the root directory of my project. What else would I need to do to have it run? I'd like to play around with the source.
all you need is those functions *and the namepsaces that come with it*
and an xml file to read and write to/from thats all
as for java sucking.. i find it very very complicated to do the simplist thing, if you created jsp pages and ejb's then you will know what i am talking about
-
Mar 28th, 2002, 11:22 PM
#19
Member
Originally posted by kovan
as for java sucking.. i find it very very complicated to do the simplist thing, if you created jsp pages and ejb's then you will know what i am talking about
Yeah I have. I don't see JSPs very hard at all, EJBs yes, but not JSPs. I guess it just depends on the application at hand. I'm just diving into EJBs so I can't speak too much about those.
-
Mar 28th, 2002, 11:28 PM
#20
Thread Starter
Frenzied Member
i see SOME usefulness in eGayb's ( )
as for GayAsp, the only thing that makes them look attractive is the custom tags,
did i mention Gayasps are very slow
asp.net is gonna blow them out of the water,
yes i see there needed to be a better solution than ASP
Gayasp's somewhat improved on that, but ASP.NET came back with enough army to take over the world...
-
Mar 28th, 2002, 11:31 PM
#21
Member
I like the way JSPs and Servlets work. I seem to get alot done in that cycle.
-
Mar 28th, 2002, 11:37 PM
#22
Thread Starter
Frenzied Member
coding them is kinda easy
but setting them up to work properly for deployment is HELL
getting to know what order you have to reference different jar files makes it hell, and not to mention there is not one single editor out there that gives helpful error messages
*jbuilder 5+ is some what good*
what am trying to say is, MS makes things so much easier for developers
i guess thats why java developers are so expensive, cus they go through hell 
i hope i never EVER program in java AGAIN
-
Mar 28th, 2002, 11:43 PM
#23
Member
As for the confusion on deployment, XML config files and so forth makes life very easy. I created a bunch of modules in JSP/Servlets a little while back for our company website (still not into production yet) and just packaged the entire site into one WAR file. I'd say the packaging system for Java is pretty helpful if you know how to use it correctly.
I agree on JBuilder. I couldn't get by without my copy of JBuilder 5.
Yeah, MS makes things easier. But that doesn't always make things better. I think each language shines in its own way. Although C# seems to have really gone far to appeal to Java developers.
-
Mar 28th, 2002, 11:51 PM
#24
Thread Starter
Frenzied Member
i liked the idea of war file and yoru done
but in order to get that war file to work was a hell
example of what i am talking about
i was experimenting with outputing my results in xml format
using Xerces and Xalan (couldnt find anything better)
no one told me or no one did i READ that you must have specific ordering of referencing libraries in order to get these to work properly
thats just one of the examples of a nightmare with java
sun has made java go down the hill, an example of this is the fact they refused to join the Web Service standard (i believe virtually ALL companies are part of this standard except sun) thats a blow to java
i personally only work with java when i am TOLD i have to
a lot of java developers phasing out java and looking into c#
-
Mar 28th, 2002, 11:57 PM
#25
Member
All the Java guys I work with don't care too much about the .NET deal at the moment. They've all got huge salaries coding Java so I can't blame them.
-
Mar 29th, 2002, 12:02 AM
#26
Thread Starter
Frenzied Member
ya, thats the problem for ms, java coders (the ones that get a lot of money) are loyal to java and probably the paycheck is whats stoping them from shifting, same with c++ programmers, they get a lot of money for something they have mastered
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|