XML to CSV using C#

Here I am going to convert an XML content into Comma Separated Values.

// strSubCategoriesXML contains



<Values>Karaikudi</Values>

<Values>Pallathur</Values>

<Values>Manachai</Values>





StringReader sReader = new StringReader(strSubCategoriesXML);

XmlReader textReader = XmlReader.Create(sReader);

StringBuilder sb = new StringBuilder();

string strCat = "";



//Reading Element Contents..

while (textReader.Read())

{

if (textReader.NodeType == XmlNodeType.Element && textReader.Name.ToString().Equals("Values"))

{

sb.Append("," + textReader.ReadElementString());

}

}



string strCat = sb.ToString().Substring(1) ; // To remove first Comma..





// strCat Contains

Karaikudi,Pallathur,Manachai

No comments: