none
Convert Xml to CSV RRS feed

  • Question

  • This is my input xml:

    <ns0:EmployeeLoad xmlns:ns0="http://BizTalk.Projects">
    <Employee>
    <EmpID>1</EmpID>
    <FirstName>Prakash</FirstName>
    <LastName>Sajwan</LastName>
    <Address>Noida</Address>
    </Employee>
    <Employee>
    <EmpID>2</EmpID>
    <FirstName>Sonika</FirstName>
    <LastName>Rawat</LastName>
    <Address>Noida,UP</Address>
    </Employee>
    </ns0:EmployeeLoad>

    I write the following code in c# to convert the xml into csv and attached it into email :

     static void Get_CSV_(XmlDocument xmlInput)
            {
               XmlDocument missingELementsDocument = new XmlDocument();
                StringBuilder strFileOutput = new StringBuilder("");
                StringBuilder str ;
                int count = 0;
                XmlNamespaceManager nameSpaceManager = new XmlNamespaceManager(xmlInput.NameTable);
                nameSpaceManager.AddNamespace("ns0","http://BizTalk.Projects");
                XmlNodeList employeeNodeList=xmlInput.SelectNodes("ns0:EmployeeLoad/Employee", nameSpaceManager);
                foreach (XmlNode childNodeOfEmployee in employeeNodeList)
                {
                    string empID = string.Empty;
                    string firstName = string.Empty;
                    string lastName = string.Empty;
                    string address = string.Empty;
                    str = new StringBuilder("");
                    empID = childNodeOfEmployee.SelectSingleNode("EmpID").InnerText;
                    firstName = childNodeOfEmployee.SelectSingleNode("FirstName").InnerText;
                    lastName = childNodeOfEmployee.SelectSingleNode("LastName").InnerText;
                    address = childNodeOfEmployee.SelectSingleNode("Address").InnerText;
                        count = count + 1;
                        if (count == 1)
                        {
                            str.Append("EmpID" + ",");
                            str.Append("FirstName" + ",");
                            str.Append("LastName" + ",");
                            str.Append("Address");
                            str.Append("\r\n");
                            
                        }
                        str.Append(empID + ",");
                        str.Append(firstName + ",");
                        str.Append(lastName + ",");
                        str.Append(address + ",");
                        strFileOutput.Append(str.ToString().Substring(0, str.ToString().Length - 1) + "\r\n");
                        str = null;
                    }

                }

    Then I send strFileOutput in attachement code.

    There is no problem in Attachemnt code.

    I am getting the csv file in attachemnt.

    My result is as follows:

    The result is correct for first employee but for second employee it is not correct.

    Due to comma(,) in address of 2nd record,UP is shifted in another cell.

    So how can I get Noida,UP in address cell ?

    Any kind of help would be appreciated.


    Prakash

    Wednesday, June 24, 2015 11:23 AM

Answers

All replies