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