473,473 Members | 2,025 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Convert Hashtable

Joe
Hi,

I have a Hashtable of string objects that I need to convert to a XML string.
Any ideas how to do this?

Thanks
Oct 27 '06 #1
6 8499

It depends on the schema of the output XML.
You could use one of the approach.
- Use StringBuilder
- Use XmlWriter
- Use XmlDocument

Could you describe the desired output XML schema?

Thi
http://thith.blogspot.com

Joe wrote:
Hi,

I have a Hashtable of string objects that I need to convert to a XML string.
Any ideas how to do this?

Thanks
Oct 27 '06 #2
Joe
Thanks for the response. The desired output is just the key as the element
with the value. Is StringBuilder the best approach?

"Truong Hong Thi" <th*****@gmail.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
>
It depends on the schema of the output XML.
You could use one of the approach.
- Use StringBuilder
- Use XmlWriter
- Use XmlDocument

Could you describe the desired output XML schema?

Thi
http://thith.blogspot.com

Joe wrote:
>Hi,

I have a Hashtable of string objects that I need to convert to a XML
string.
Any ideas how to do this?

Thanks

Oct 27 '06 #3
Hi,

The StringBuilder is simple and best approach to your requirements.
I guess the desired output is like this:
<key>value</key>
If so, make sure the key is a valid XML identifier.
The safer way is <item key="key">value</item>.
Sample code:
const string pattern = "<item key=\"{0}\">{1}</item>";
StringBuilder buffer = new StringBuilder("<table>");
foreach (DictionaryEntry entry in theHashTable)
{
buffer.AppendFormat(pattern, entry.Key, entry.Value);
}
buffer.Append("</table>");
return buffer.ToString();

Hope it helps,
Thi
http://thith.blogspot.com

Joe wrote:
Thanks for the response. The desired output is just the key as the element
with the value. Is StringBuilder the best approach?
Oct 27 '06 #4
IMO, using StringBuilder directly is a fairly sure way of introducing
an occasional bug... all you need is a reserved xml character in either
the key or value, and it's broken. That's way too brittle for my
liking.

You can, however, do something like below; fairly hard to break this
via content... also, note that the order of items is not guaranteed by
Hashtable

Marc

Hashtable ht = new Hashtable();
ht.Add("Test", "Something");
ht.Add("Whatever", "abcf");
ht.Add("asd", "adsfgaa");

StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
{
writer.WriteStartElement("items");
foreach (DictionaryEntry item in ht)
{
writer.WriteStartElement("item");
writer.WriteAttributeString("key",
item.Key.ToString());
writer.WriteValue(item.Value);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.Flush();
}
Console.WriteLine(sb.ToString());

Oct 27 '06 #5
Yes, that is safer. XmlWriter should know how to escape illegal
characters.

Oct 27 '06 #6
Joe
Thanks for the posts. That makes sense to use XMLWriter and I appreciate
the help.

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11********************@h48g2000cwc.googlegrou ps.com...
IMO, using StringBuilder directly is a fairly sure way of introducing
an occasional bug... all you need is a reserved xml character in either
the key or value, and it's broken. That's way too brittle for my
liking.

You can, however, do something like below; fairly hard to break this
via content... also, note that the order of items is not guaranteed by
Hashtable

Marc

Hashtable ht = new Hashtable();
ht.Add("Test", "Something");
ht.Add("Whatever", "abcf");
ht.Add("asd", "adsfgaa");

StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
{
writer.WriteStartElement("items");
foreach (DictionaryEntry item in ht)
{
writer.WriteStartElement("item");
writer.WriteAttributeString("key",
item.Key.ToString());
writer.WriteValue(item.Value);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.Flush();
}
Console.WriteLine(sb.ToString());

Oct 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: francois | last post by:
First of all I would to to apologize for resending this post again but I feel like my last post as been spoiled Here I go for my problem: Hi, I have a webservice that I am using and I would...
5
by: Cyrus | last post by:
I have a question regarding synchronization across multiple threads for a Hashtable. Currently I have a Threadpool that is creating worker threads based on requests to read/write to a hashtable....
33
by: Ken | last post by:
I have a C# Program where multiple threads will operate on a same Hashtable. This Hashtable is synchronized by using Hashtable.Synchronized(myHashtable) method, so no further Lock statements are...
2
by: TC | last post by:
Does anyone know how to convert a hashtable to Hex under C#? // Return a base 64 encoded string of the Hash value return Convert.ToBase64String(bytHash); Thanks & Regards, TC
3
by: Fred | last post by:
I'm trying to build a hashtable and a arraylist as object value I'm not able to retrieve stored object from the hashtable. Hashtable mp = new Hashtable(); // THE HASHTABLE ArrayList...
8
by: asrs63 | last post by:
Hi, I am a newbee and have a comma seperated flat-file and a DTD. I need to write a C# program which will read the text file and convert it to a XML file as per the the definition in the DTD. I...
2
by: PAzevedo | last post by:
I have this Hashtable of Hashtables, and I'm accessing this object from multiple threads, now the Hashtable object is thread safe for reading, but not for writing, so I lock the object every time I...
4
by: Chris Bordeman | last post by:
I have a DataColumn, want to derive the DbType. I can do column.GetType() but that's a system type, not a db type. How do I convert it to the corresponding type? Thanks much! Chris B.
15
by: Buddy Home | last post by:
Hello, I'm trying to speed up a piece of code that is causing performance issues with our product. The problem is we are using serialization to convert the object to a string, this is costing us...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.