Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 3rd, 2006, 12:10 PM
Authorized User
 
Join Date: Sep 2005
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Default File to Hashtable

Hi, i want to use a Hashtable that is initialized with information that's in a file
I don't want to be reading the file and then adding everything in the Hashtable, this sound like a lot of work
I Java, i have

Properties z = new Properties();
try {
   z.load(new FileInputStream("d:\\zipHash.props"));
}
catch(Exception x) {
   System.out.println("Error: " + x.getMessage());
}

And the files is organized with key= value

Is there something like this in c# ?
Thanks

 
Old February 14th, 2006, 05:39 AM
Registered User
 
Join Date: Feb 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I don't know how you plan to use the "z" object in your code. Is it a one-off requirement or a regular feature. I have listed some possibilities below along with my observations.

In C# the same can be achieved via the following code:
//namespaces required

using System.Data;

//code snippet

DataSet myDS = new DataSet();
try
{
   myDS.ReadXml(@"c:\hashtest.xml");
   DataRow z = ds.Tables[0].Rows[0];
}
catch(Exception ex)
{
   Response.Write(ex.Message);
}
finally
{
   myDS = null;
}


The contents of hashtest.xml are given below,

<?xml version="1.0" encoding="utf-8" ?>
<hashtable>
<key1>Value1</key1>
<key2>Value2</key2>
<key3>Value3</key3>
</hashtable>

While there is no ready made feature like Properties of Java. The above code could help you achieve the same.

I am not much of a Java guy, but have worked a little on Java and would suggest not to try and replicate Java style programming in C#. C# has better ways of doing the same thing that are done in a particular manner in Java.

Even looking at the sample code you have presented below,

z.load(new FileInputStream("d:\\zipHash.props"));

you would realize that load method would actually load the file and add it to the hash table, albeit internally.

I would suggest the following as a better alternative,


Hashtable z = new Hashtable();
XmlDocument myXmlDoc = new XmlDocument();
myXmlDoc.Load(@"C:\hashtest.xml");
XmlNodeList nList = myXmlDoc.GetElementsByTagName("Hashtable");
foreach(XmlNode xNode in nList)
{
    z.Add(xNode.FirstChild.FirstChild.FirstChild.Value , xNode.FirstChild.LastChild.FirstChild.Value);
}

the following is the xml structure for the above code snippet,

<?xml version="1.0" encoding="utf-8" ?>
<Hashtable>
    <object>
        <key>key1</key>
        <value>value1</value>
    </object>
    <object>
        <key>key2</key>
        <value>value2</value>
    </object>
    <object>
        <key>key3</key>
        <value>value3</value>
    </object>
</Hashtable>

Incase, you have a regular requirement you could make your own custom "Properties" class and hide the extra steps as the Java class has done.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Hashtable ajit Java Basics 1 July 11th, 2006 01:25 AM
Hashtable Scott Rider General .NET 7 July 3rd, 2005 02:25 AM
Hashtable mahulda General .NET 2 August 2nd, 2004 07:49 AM
Hashtable in C# sachin-csharp .NET Framework 2.0 0 July 28th, 2004 01:45 AM
about hashtable csc820203 C# 1 July 11th, 2004 07:44 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.