Hi - I have an xml document that has the following data.

<?xml version="1.0" encoding="utf-8"?>
<ValidateClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<addr1Valid>73 McKNIGHT RD</addr1Valid>
<addr2Valid />
<cityValid>PITTSBURGH</cityValid>
<stateValid>P</stateValid>
<stateZip>S15237351</stateZip>
</ValidateClass>

I am trying to convert the xml data to comma delimited string. for eg:

73 McKNIGHTRD,null,PITTSBURGH,P,S15237351

I' m trying to accompolish this using StringWriter and it didnot give the result the way I want. I have tried formating and indentation that did not help.

It just gave the result as
73 McKNIGHTRD null PITTSBURGH P S15237351 (no comma)

please help me.

Recommended Answers

All 5 Replies

Could you please post that part of the code that uses StringBuilder?

Hi - Sure, Here is my code( I didn't use StringBuilder)

XmlDocument xmlDoc = new XmlDocument();
                            try
                            {
                                xmlDoc.Load(file);
                            }
                            catch (XmlException e)
                            {
                                Console.WriteLine(e.Message);
                            }
StringWriter sw=new StringWriter();

                            // Now create StringWriter object to get data from xml document.
                             XmlTextWriter xw = new XmlTextWriter(sw);
                          
                            
                            xw.Formatting = Formatting.Indented;
                            xw.Indentation = 4;
                            

                            xmlDoc.WriteTo(xw);
                            sw.Write(".\n\n");

Pleae help me how would I get the result

73 McKNIGHTRD,null,PITTSBURGH,P,S15237351.

Currently, using the above code I get
73 McKNIGHTRD null PITTSBURGH P S15237351.( without comma)

If I could get a result from xml document as seperate elements such as
73 McKNIGHTRD
null
PITTSBURGH
P
S15237351

that would be great instead of comma seperated string. I am trying to show these fields from C# to sql table. Please let me know your suggestions as well. Thanks

I didn't use StringBuilder

Sorry, my mistake. I read the question wrong.

Have you tried to set IndentChar property:

.
.
xw.Indentation = 1;
xw.IndentChar = ',';
.
.

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.