views:

778

answers:

4

I am trying to create an XML file based on data fields from a table, and I want to have the nodes named based on the value in a field from the table. The problem is that sometimes values entered in that column contain spaces and other characters not allowed in Node names.

Does anyone have any code that will cleanup a passed in string and repalce invalid characters with replacement text so that it can be reversed on the other end and get the original value back?

I am using .net (vb.net but I can read/convert c#)

+1  A: 

It might be easier if you stored the original value if it were illegal as a node name in an attribute. Then you wouldn't worry about having some sort of complex to/from translation.

spoon16
very good tip about storing the original value
MikeScott8
+1  A: 

As a matter of fact I would go so far as to say that unless you have complete control over the data, then no translation process would ever work. So I second storing the original data either as an attribute or a child node.

EBGreen
you are right and we ended up not doing the translation
MikeScott8
+1  A: 

You didn't say in your original post what language so here's a regex pattern that should get you started. This is QUICK so you'll need to test it.

([^A-Za-z0-9])|(\s)|(\t+)|(\c+)
Chuck
thanks for that regex and I will play with it and see what I can make of it.
MikeScott8
YW. I made a slight change to it. Again, this is quick and I definitely didn't test it. hopefully it will help.
Chuck
I think that regex will probably work, but how do you plan to do the translation back?
EBGreen
That is the problem. The translation back is difficult and I think error prone. That's why I recommended storing the original value in some way (as an attribute value).
spoon16
A: 

I agree with @spoon16.