Showing posts with label Problem & Solutions. Show all posts
Showing posts with label Problem & Solutions. Show all posts

Saturday, January 4, 2014

Convert Xml in to CSV in c#


In this article we are going to see how to convert the xml in to CSV format, based on the custom delimiters.

Files:

Source code for Convert Xml in to CSV:

  class XmlToCsv
    {
        public enum RowDelimit
        {
            Default,
            NewLine,
            Space,
            Ambescent,
            Dollar
        }
        public enum ColumnDelimit
        {
            Default,
            Comma,
            TabSpace,
            Percentage,
            OrSymbol,
            Dot
        }
        public enum DataArrange
        {
            Element,
            Attribute
        }

        public static RowDelimit RowDelimiter { set; get; }

        public static ColumnDelimit ColumnDelimiter { set; get; }

        public static void FetchRowSeparater(RowDelimitdelimit, out stringseparater)
        {
            switch (delimit)
            {
                case RowDelimit.NewLine:
                case RowDelimit.Default:
                    separater = Environment.NewLine;
                    break;
                case RowDelimit.Space:
                    separater = " ";
                    break;
                case RowDelimit.Dollar:
                    separater = "$";
                    break;
                case RowDelimit.Ambescent:
                    separater = "&";
                    break;
                default:
                    separater = Environment.NewLine;
                    break;
            }
        }


        public static void FetchColumnSeparater(ColumnDelimitdelimit, out stringseparater)
        {
            switch (delimit)
            {
                case ColumnDelimit.Comma:
                case ColumnDelimit.Default:
                    separater = ",";
                    break;
                case ColumnDelimit.Dot:
                    separater = ".";
                    break;
                case ColumnDelimit.OrSymbol:
                    separater = "|";
                    break;
                case ColumnDelimit.Percentage:
                    separater = "%";
                    break;
                case ColumnDelimit.TabSpace:
                    separater = "\t";
                    break;
                default:
                    separater = ",";
                    break;
            }
        }

        public static void Convert(stringxmlfilepath, string csvpath, string datatag, DataArrangearrange, RowDelimit rdelimit, ColumnDelimit cdelimit)
        {
            try
            {
                StringBuilder builder = new StringBuilder();

                XDocument doc = XDocument.Load(xmlfilepath);
                string Rowseparater = string.Empty;
                FetchRowSeparater(rdelimit, outRowseparater);

                string Columnseparater = string.Empty;
                FetchColumnSeparater(cdelimit, outColumnseparater);

                foreach (XElementdata in doc.Descendants(datatag))
                {
                    if (arrange == DataArrange.Element)
                    {
                        foreach (XElement innnerval in data.Elements())
                        {
                            builder.Append(innnerval.Value);
                            builder.Append(Columnseparater);
                        }
                    }
                    else
                    {
                        foreach (XAttribute innerval in data.Attributes())
                        {
                            builder.Append(innerval.Value);
                            builder.Append(Columnseparater);
                        }
                    }
                    builder.Append(Rowseparater);
                }

                File.AppendAllText(csvpath, builder.ToString());
            }
            catch
            {
                throw;
            }
        }
    }



Input Xml File:

<Employees>
<Employee name="Rajesh" address="Chennai" dept="IT" />
<Employee name="Suresh" address="Chennai" dept="CSE" />
<Employee name="Siva" address="US" dept="R&amp;D" />
</Employees>





Read the xml which have values in Attributes and convert that into CSV.

          try
            {
                XmlToCsv.Convert(@"D:\data\attrxml.xml", @"D:\data\rowxml.csv", "Employee", XmlToCsv.DataArrange.Attribute, XmlToCsv.RowDelimit.NewLine, XmlToCsv.ColumnDelimit.Comma);
                Console.Write("Done");
            }
            catch (Exceptionex)
            {
                Console.Write(ex.Message);
            }
            Console.Read();

Read the xml which have values in Elements and convert that into CSV.

Input Xml File:

<Employees>
<Employee>
    <name>Rajesh</name>
    <address>Chennai</address>
    <dept>IT</dept>
</Employee>
<Employee>
    <name>Suresh</name>
    <address>Chennai</address>
    <dept>CSE</dept>
</Employee>
<Employee>
    <name>Siva</name>
    <address>US</address>
    <dept>R&amp;D</dept>
</Employee>
</Employees>


            try
            {
                XmlToCsv.Convert(@"D:\data\elementxml.xml", @"D:\data\elemntxml.csv", "Employee", XmlToCsv.DataArrange.Element, XmlToCsv.RowDelimit.NewLine, XmlToCsv.ColumnDelimit.Comma);
                Console.Write("Done");
            }
            catch (Exceptionex)
            {
                Console.Write(ex.Message);
            }
            Console.Read();


Output:

Rajesh,Chennai,IT,
Suresh,Chennai,CSE,
Siva,US,R&D,


You can change the delimiters based on your requirements.


From this post you can understand how to implement how to xml to csv format.

Thursday, December 12, 2013

FTP - 421 , Clear text sessions are not accepted on this server [Resolved]

      Hi Guys , In this article we are going to see some ftp issue and the resolution for that. you will faced this issue once a time while working in the FTP. While connecting a FTP Server from you will see a Error message like

421 - Clear text session are not accepted on this server

How to solve this issue Let we see now ?
 Just add ftpes in front of your domain server thats it ex: "ftpes://ftp.domain.com" This will resolve the issue



ftpes://ftp.domain.com

In Old Server : 
server : ftp.domain.com
username : user
password : xxxx

In New Server:

server :ftpes://ftp.domain.com
username : user
password : xxxx

Please check that TLS Encryption support is set to Optional in FTP Service Configuration.I Hope this article will help some of them to do there work in quick manner.

Saturday, August 3, 2013

Asynchronous programming with Async and Await

Let we see the simple implementation of async and await opertor in C# code.






1. Always async should be mention in method after access specifier.
2. Method name should be end in async.
3. Return type of async method be in void, Task, Task<T>

                The first thing is to know is that any method that you put async in front of is an asynchronous method, which means it can be started and stopped rather than just run from first to last instruction. We could create a new method and mark is as asynchronous but to keep the example as much like the synchronous case described above we can simply change the Click event handler into an asynchronous method:

private async void button1_Click(object sender, RoutedEventArgs e)
{
 
label1..Text = "Started";
 DoWork();
 label2.Text = "Finished";
}

private int DoWork()
{
 return 1+2;
}

Now the compiler will say a message as Asynchronous method with not Await, make to run the method in synchronous way, So i have added awaitkeyword in calling a method.

private async void button1_Click(object sender, RoutedEventArgs e)
{
 
label1..Text = "Started";
 await DoWork();
 label2.Text = "Finished";
}

private int DoWork()
{
 return 1+2;
}

Now compiler will say a message that you can't await a method that returns void. we have to change the return type as Task or Task<T>.Now let we see the Task 

What is Task ?
    Task is an object that runs the code in separate thread from the UI thread which release the UI to do some other work.

Now we see the implemetation of await with task return type
private async void button1_Click(object sender, RoutedEventArgs e)
{
 
label1..Text = "Started";
 await Task.Run(()=> DoWork());
 label2.Text = "Finished";
}

private int DoWork()
{
 return 1+2;
}

From this article I hope you will understand the async and await operation clearly when comparing to synchronous operation, and learn to change a call sync method to await call of Task return type method.



Saturday, July 27, 2013

Split Function - User Defined function in SQL SERVER with multiple split conditions



     In this article we are going to see a split function in SQL SERVER. How the split function works. The first parameter for the function takes the word and second parameter takes the group of condition based on words has to be split  “.,@#$ %^” etc.

For Example: Rajesh,…Is  a,C##Developer
Output:
Rajesh
Is
A
C#
Developer

Let we see the function Now,

CREATE FUNCTION [DBO].[SPLIT]
(
@DATA NVARCHAR(MAX),
@SPLITCONDITION NVARCHAR(30)
)
RETURNS @VALUE TABLE(TEXT NVARCHAR(MAX))
AS
BEGIN
DECLARE @CONLEN INT = LEN (@SPLITCONDITION)

      IF @CONLEN < 1
      BEGIN
            INSERTINTO @VALUE(TEXT) SELECT @DATA
            RETURN
      END
      ELSE
      BEGIN       
            DECLARE@LEN  INT= LEN(@DATA)      
            DECLARE@LOOP INT = 0
            DECLARE@TEMPDATA NVARCHAR(MAX)
            DECLARE@CHAR CHAR(1)
            SELECT@TEMPDATA = ''
                       
            WHILE(@LOOP <= @LEN)
            BEGIN
                  SELECT@CHAR = SUBSTRING(@DATA,@LOOP,1)
                 
                  IFCHARINDEX(@CHAR,@SPLITCONDITION) > 0
                  BEGIN
                  IF  @TEMPDATA <>''
                   BEGIN
                        INSERTINTO @VALUE(TEXT) SELECT @TEMPDATA
                        SELECT@TEMPDATA = ''
                    END
                  END
                  ELSE 
                  BEGIN 
                        SELECT@TEMPDATA = @TEMPDATA +@CHAR
                  END
                  SELECT@LOOP = @LOOP+1
            END
                  IF@TEMPDATA <>''
                  INSERTINTO @VALUE(TEXT) SELECT @TEMPDATA                   
            RETURN
      END
      RETURN
END

How to call the Split function ?
SELECT TEXT FROM DBO.SPLIT('C#@IS,A,.OBJECT ORIENTED$LANGUAGE.,','.,$@ &')

Output :


TEXT
1
C#
2
IS
3
A
4
OBJECT
5
ORIENTED
6
LANGUAGE


From this article we can see how the user defined split function is created in SQL server and executed.