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.

Edited 5 Years Ago by madhan: n/a

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");

Edited 5 Years Ago by madhan: n/a

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

Edited 5 Years Ago by madhan: n/a

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

This article has been dead for over six months. Start a new discussion instead.