text parsing application c# without third party libraries

convert xml to json c# without newtonsoft
deserialize json c#
javascriptserializer
c# json parser
read pdf file in c# without using itextsharp
newtonsoft json
json to c#
convert response to json c#

For example, there is a line:

name, tax, company.

To separate them i need a split method.

  string[] text = File.ReadAllLines("file.csv", Encoding.Default);
   foreach (string line in text)
    {
     string[] words = line.Split(',');
     foreach (string word in words)
      {
       Console.WriteLine(word);
      }
     }
   Console.ReadKey();

But how to divide if in quotes the text with a comma is indicated:

name, tax, "company, Ariel";<br>
"name, surname", tax, company;<br>  and so on.

To make it like this :

  Max | 12.3 | company, Ariel
Alex, Smith| 13.1 | Oriflame

It is necessary to take into account that the input data will not always be in an ideal format (as in the example). That is, there may be 3 quotes in a row or a string without commas. The program should not fall in any case. If it is impossible to parse, then issue a message about it.

Split using double quotes first. And Split using comma on the first string.

Parsing JSON without external library - MSDN, Is it possible to parse JSON in C# without using any external library? Friday, October 9, 2015 1:06 PM. Reply. |. Quote. Avatar of Amit_. Amit_  Video Tutorials C# Fundamentals: Development for Absolute Beginners. Useful MSDN Resources A Tour of the C# Language Get started with .NET in 10 minutes C# Guide C# Language Reference C# Programing Guide C# Coding Conventions.NET Framework Reference Source Code. Other Resources C# Yellow Book Dot Net Perls C# Discord Group

You can use TextFieldParser from Microsoft.VisualBasic.FileIO

var list = new List<Data>();
var isHeader=true;
using (TextFieldParser parser = new TextFieldParser(filePath))
{

        parser.Delimiters = new string[] { "," };
        while (true)
        {
            string[] parts = parser.ReadFields();
            if(isHeader)
            {
                isHeader = false; 
                continue;
            }
            if (parts == null)
                break;

            list.Add(new Data
                {
                    People = parts[0],
                    Tax = Double.Parse(parts[1]),
                    Company = parts[2]
                });

        }
 }

Where Data is defined as

public class Data
{
    public string People{get;set;}
    public double Tax{get;set;}
    public string Company{get;set;}
}

Please note you need to include Microsoft.VisualBasic.FileIO

Example Data,

Name,Tax,Company
Max,12.3,"company, Ariel"
Ariel,13.1,"company, Oriflame"

Output

Adaptive Code via C#: Agile coding with design patterns and SOLID , DAVID: STEVE: I assume I should be using a third-party library for parsing markdown David sets to work on implementing the markdown transform for the application. I knew I wanted to intercept the room message text by implementing a  Return AST from grammar and parsed tree that is built from any text. Work fast enough to load C# grammar then parse a large code file. I'd prefer the library that has grammar format file simple enough for easy writing a grammar for math expressions, is open source and written in C# or C++. Regards,-- UPDATED: point 2 has been corrected.

Here's a bit of code that might help, not the most efficient but I use it to 'see' what is going on with the parsing if a particular line is giving trouble.

string[] text = File.ReadAllLines("file.csv", Encoding.Default);
string[] datArr;
string tmpStr;
foreach (string line in text)
{
  ParseString(line, ",", "!@@@@!", out datArr, out tmpStr)
  foreach(string s in datArr)
  {
    Console.WriteLine(s);
  }
}
Console.ReadKey();

private static void ParseString(string inputString, string origDelim, string newDelim, out string[] retArr, out string retStr)
{
    string tmpStr = inputString;
    retArr = new[] {""};
    retStr = "";

    if (!string.IsNullOrWhiteSpace(tmpStr))
    {
        //If there is only one Quote character in the line, ignore/remove it:
        if (tmpStr.Count(f => f == '"') == 1)
            tmpStr = tmpStr.Replace("\"", "");

        string[] tmpArr = tmpStr.Split(new[] {origDelim}, StringSplitOptions.None);
        var inQuote = 0;

        StringBuilder lineToWrite = new StringBuilder();
        foreach (var s in tmpArr)
        {
            if (s.Contains("\""))
                inQuote++;

            switch (inQuote)
            {
                case 1:
                    //Begin quoted text
                    lineToWrite.Append(lineToWrite.Length > 0
                        ? newDelim + s.Replace("\"", "")
                        : s.Replace("\"", ""));

                    if (s.Length > 4 && s.Substring(0, 2) == "\"\"" && s.Substring(s.Length - 2, 2) != "\"\"")
                    {
                        //if string has two quotes at the beginning and is > 4 characters and the last two characters are NOT quotes,
                        //inquote needs to be incremented.
                        inQuote++;
                    }
                    else if ((s.Substring(0, 1) == "\"" && s.Substring(s.Length - 1, 1) == "\"" &&
                              s.Length > 1) || (s.Count(x => x == '\"') % 2 == 0))
                    {
                        //if string has more than one character and both begins and ends with a quote, then it's ok and counter should be reset.
                        //if string has an EVEN number of quotes, it should be ok and counter should be reset.
                        inQuote = 0;
                    }
                    else
                    {
                        inQuote++;
                    }

                    break;
                case 2:
                    //text between the quotes
                    //If we are here the origDelim value was found between the quotes
                    //include origDelim so there is no data loss.
                    //Example quoted text: "Dr. Mario, Sr, MD";
                    //      ", Sr" would be handled here
                    //      ", MD" would be handled in case 3 end of quoted text.
                    lineToWrite.Append(origDelim + s);
                    break;
                case 3:
                    //End quoted text
                    //If we are here the origDelim value was found between the quotes
                    //and we are at the end of the quoted text
                    //include origDelim so there is no data loss.
                    //Example quoted text: "Dr. Mario, MD"
                    //      ", MD" would be handled here.
                    lineToWrite.Append(origDelim + s.Replace("\"", ""));
                    inQuote = 0;
                    break;
                default:
                    lineToWrite.Append(lineToWrite.Length > 0 ? newDelim + s : s);
                    break;

            }

        }

        if (lineToWrite.Length > 0)
        {
                retStr = lineToWrite.ToString();
                retArr = tmpLn.Split(new[] {newDelim}, StringSplitOptions.None);

        }

    }
}

Encyclopedia of Computer Science and Technology, C/C++ External support PHP Common API (PDO) C# Common API (ADO. Almost all GPLs provide standard libraries to support XML. C and C++ are perhaps the only popular languages that depend on third-party libraries for XML parsing and Implement the application library: Creation of an application library that  There are no extention points in the compiler for the lexing/parsing stage, so you'll either have to parse your self or use a third party parser/lexer (ANTLR is pretty decent, free and even has a couply of C# grammars available) if you never deald with lexing and parsing before the learning curve can be pretty steep, if you are interested in some theory The Dragon book will satisfy those needs

How to read PDF without using any DLL in C# or VB, If that is not the case, then go and get a free library (there are many out If you can build an application, that loads the browser with the PDF file  I really would like to do this without incorporating a 3rd party tool as my use case really doesn't involve many other cases besides this one and even though it is part of my work's solution, having a tool like that incorporated doesn't really benefit anyone at the moment.

Game Programming Algorithms and Techniques: A Platform-Agnostic , There are several third-party libraries that can parse JSON, including libjson (http​://libjson. sourceforge.net) for C++ and JSON.NET for C# and other . So although JSON can be a great text-based file format in some situations, in the case of  How do you extract text from dynamic HTML without using 3rd party libraries? Simple, you invent your own HTML parsing library using the string parsing functions present in the .NET framework. Seriously, doing this by yourself is a bad idea.

Parsing CSV files in .NET without any third party packages, Home > C# > Parsing CSV files in .NET without any third party packages foreach ( string line in File. I searched around, and found a few libraries and Nuget packages that offered all sorts of exotic and impressive features,  One of the best way some people help, is to open source their hard work into libraries, so you don’t have to code the behavior yourself. It’s always hard to know what’s out there, so in this post, I want to give a shootout to some of the .NET libraries I find could definitely enhance your application(s) and if not, beef up your toolbox.

Comments
  • Hi, if you want to write your own parser (there is a csv parser somewhere in the .net framework), you cannot use the split, because of the problem you stated. instead of this, you have to check each character for a delimiter character, and take care of if you are within an open " or not. Not really tricky.
  • It is in Microsoft.VisualBasic.FileIO.TextFieldParser (you can use it from C# ...)
  • I would recommend not trying to write your own parser, there are decent ones out there like CSVHelper which will save you headaches
  • @NDJ: Not beeing aware of the Framework Field Parser, I wrote one day my own parser ... was really easy, no problem. No headaches, and more flexible than a "standard" tool (at least when I had some ... strange ideas ... of how the csv could also be formatted)
  • Possible duplicate of How can I Split(',') a string while ignore commas in between quotes?
  • This will not work, if you have ALSO " within a string escaped as "" or other kind of escape char (like \" ...)
  • Agree for a global parser it would not always work. But it doesn't look like that is a potential fail in that data. As far as I assume from the sample data.
  • Thanks, but I cannot use third-party libraries for parsing CSV, or Microsoft.VisualBasic.FileIO this library not a third-party?
  • @vamp123 That's from microsoft i suppose. You can find more details here. docs.microsoft.com/en-us/dotnet/api/…