示例#1
0
        /// <summary>
        /// Method ToParseXML
        /// parses XML-file into list
        /// </summary>
        /// <returns>list with data from  XML-file</returns>

        public List <string> ToParseXML()
        {
            //convert XML-file into array of strings
            ConvertIntoStrings converter = new ConvertIntoStrings(adressOfFile);

            string[] fileInStrings = converter.fileInString;

            //list in which we will keep data from XML-file
            List <string> dataOfXMLFile = new List <string>();

            //index of symbol in string
            int indOfStr = 1;

            //in this string we will keep data from XML-string
            StringBuilder elementOfXML = new StringBuilder();

            //object for work with strings (add symbols, delete...)
            WorkWithString builder = new WorkWithString();

            //start of JSON-file
            dataOfXMLFile.Add("{");

            //start to parse XML-file
            for (int i = 1; i < fileInStrings.Length; i++)
            {
                string str = fileInStrings[i];

                //delete spaces from current string
                string currentString = builder.RemoveSpacesAtBeginning(str);

                // if string contains "<?xml"(XML-document declaration)
                // or "<!" (comments)
                // then skip this string
                if (currentString.StartsWith("<?xml") || currentString.StartsWith("<!"))
                {
                    continue;
                }

                // if string contains "</"
                // then it is a closing tag
                // and prints "}" into JSON-file
                if (currentString.StartsWith("</"))
                {
                    elementOfXML.Append("}");
                    dataOfXMLFile.Add(elementOfXML.ToString());
                    elementOfXML.Clear();
                    continue;
                }

                // if string contains "<" and "</"
                // then prints into JSON-file next
                // "elementOfXML": "value",
                if (currentString.StartsWith("<") && currentString.Contains("</"))
                {
                    elementOfXML.Append("   ");
                    elementOfXML.Append('"');
                    while (currentString[indOfStr] != '>')
                    {
                        elementOfXML.Append(currentString[indOfStr]);
                        indOfStr++;
                    }
                    elementOfXML.Append('"');
                    elementOfXML.Append(": ");
                    elementOfXML.Append('"');

                    indOfStr++;

                    while (currentString[indOfStr] != '<')
                    {
                        elementOfXML.Append(currentString[indOfStr]);
                        indOfStr++;
                    }
                    elementOfXML.Append('"');
                    elementOfXML.Append(',');
                    dataOfXMLFile.Add(elementOfXML.ToString());
                    elementOfXML.Clear();
                    indOfStr = 1;

                    continue;
                }

                // if string contains only "<elementOfXML>"
                // then bypass the whole block to the end
                if (currentString.StartsWith("<"))
                {
                    elementOfXML.Append("   ");
                    elementOfXML.Append('"');
                    while (currentString[indOfStr] != '>')
                    {
                        elementOfXML.Append(currentString[indOfStr]);
                        indOfStr++;
                    }
                    elementOfXML.Append('"');
                    elementOfXML.Append(": {");
                    dataOfXMLFile.Add(elementOfXML.ToString());
                    indOfStr = 1;
                    elementOfXML.Clear();

                    i++;

                    str = fileInStrings[i];

                    currentString = builder.RemoveSpacesAtBeginning(str);

                    // while did not reach the end of block
                    while (!currentString.StartsWith("</"))
                    {
                        // if string contains "<" and "</"
                        // then prints into JSON-file next
                        // "elementOfXML": "value",
                        if (currentString.StartsWith("<") && currentString.Contains("</"))
                        {
                            elementOfXML.Append("       ");
                            elementOfXML.Append('"');
                            while (currentString[indOfStr] != '>')
                            {
                                elementOfXML.Append(currentString[indOfStr]);
                                indOfStr++;
                            }
                            elementOfXML.Append('"');
                            elementOfXML.Append(": ");

                            indOfStr++;

                            elementOfXML.Append('"');
                            while (currentString[indOfStr] != '<')
                            {
                                elementOfXML.Append(currentString[indOfStr]);
                                indOfStr++;
                            }
                            elementOfXML.Append('"');
                            elementOfXML.Append(',');
                            dataOfXMLFile.Add(elementOfXML.ToString());
                            elementOfXML.Clear();
                            indOfStr = 1;
                        }
                        i++;

                        str = fileInStrings[i];
                        //delete spaces from current string
                        currentString = builder.RemoveSpacesAtBeginning(str);
                    }

                    // if have reached to the end of block
                    // then print into JSON-file "},"
                    if (currentString.StartsWith("</"))
                    {
                        elementOfXML.Append("   ");
                        elementOfXML.Append("},");
                        dataOfXMLFile.Add(elementOfXML.ToString());
                        elementOfXML.Clear();
                        indOfStr = 1;
                    }
                    continue;
                }
            }
            return(dataOfXMLFile);
        }
示例#2
0
        /// <summary>
        /// Method ToParseJSON
        /// parses JSON-file into list
        /// </summary>
        /// <returns>list with data from  JSON-file</returns>

        public List <string> ToParseJSON()
        {
            //convert JSON-file into array of strings
            ConvertIntoStrings converter = new ConvertIntoStrings(adressOfFile);

            string[] JSONInStrings = converter.fileInString;

            //list in which we will keep data from JSON-file
            List <string> dataOfJSONFile = new List <string>();

            //index of symbol in string
            int indOfStr = 1;

            //in this string we will keep the way into data
            StringBuilder dataOfJSON = new StringBuilder();

            //object for work with strings (add symbols, delete...)
            WorkWithString builder = new WorkWithString();

            //element of JSON-file
            StringBuilder element = new StringBuilder();
            //element inside the block in JSON-file
            StringBuilder nestedElement = new StringBuilder();
            //value of nested element
            StringBuilder value = new StringBuilder();

            //start of XML-file
            dataOfJSONFile.Add("<object>");

            //start to parse JSON-file
            for (int i = 1; i < JSONInStrings.Length; i++)
            {
                string str = JSONInStrings[i];
                //delete spaces from current string
                string currentString = builder.RemoveSpacesAtBeginning(str);

                // if current string contains string field
                // then print into XML-file next
                // <element>value of element</element>
                if (!currentString.Contains("{") && !currentString.Contains("}") && !currentString.Contains("["))
                {
                    dataOfJSON.Append("   ");
                    dataOfJSON.Append('<');

                    while (currentString[indOfStr] != '"')
                    {
                        element.Append(currentString[indOfStr]);
                        indOfStr++;
                    }
                    dataOfJSON.Append(element);
                    dataOfJSON.Append('>');

                    //": " - we need to pass 4 symbols to get value of element
                    indOfStr = indOfStr + 4;

                    while (currentString[indOfStr] != '"')
                    {
                        dataOfJSON.Append(currentString[indOfStr]);
                        indOfStr++;
                    }
                    //prints closing tag
                    dataOfJSON.Append("</");
                    dataOfJSON.Append(element);
                    dataOfJSON.Append(">");

                    dataOfJSONFile.Add(dataOfJSON.ToString());
                    dataOfJSON.Clear();
                    element.Clear();
                    indOfStr = 1;

                    continue;
                }

                // if string contains object
                // then bypass the whole block to the end
                if (currentString.Contains("{"))
                {
                    dataOfJSON.Append("   ");
                    dataOfJSON.Append("<");

                    while (currentString[indOfStr] != '"')
                    {
                        element.Append(currentString[indOfStr]);
                        indOfStr++;
                    }
                    dataOfJSON.Append(element);
                    dataOfJSON.Append(">");
                    dataOfJSONFile.Add(dataOfJSON.ToString());
                    dataOfJSON.Clear();
                    indOfStr = 1;

                    i++;
                    str = JSONInStrings[i];
                    //delete spaces from current string
                    currentString = builder.RemoveSpacesAtBeginning(str);

                    //prints nested elements of object
                    while (!currentString.StartsWith("},"))
                    {
                        if (!currentString.Contains("{"))
                        {
                            dataOfJSON.Append("        ");
                            dataOfJSON.Append('<');

                            while (currentString[indOfStr] != '"')
                            {
                                nestedElement.Append(currentString[indOfStr]);
                                indOfStr++;
                            }
                            dataOfJSON.Append(nestedElement);
                            dataOfJSON.Append('>');

                            indOfStr = indOfStr + 4;

                            while (currentString[indOfStr] != '"')
                            {
                                dataOfJSON.Append(currentString[indOfStr]);
                                indOfStr++;
                            }
                            dataOfJSON.Append("</");
                            dataOfJSON.Append(nestedElement);
                            dataOfJSON.Append(">");

                            dataOfJSONFile.Add(dataOfJSON.ToString());
                            dataOfJSON.Clear();
                            nestedElement.Clear();
                            indOfStr = 1;
                        }
                        i++;

                        str = JSONInStrings[i];
                        //delete spaces from current string
                        currentString = builder.RemoveSpacesAtBeginning(str);
                    }

                    // if we have reached the end of block in JSON-file
                    // then prints closing tag
                    if (currentString.StartsWith("},"))
                    {
                        dataOfJSON.Append("   ");
                        dataOfJSON.Append("</");
                        dataOfJSON.Append(element);
                        dataOfJSON.Append(">");

                        dataOfJSONFile.Add(dataOfJSON.ToString());
                        dataOfJSON.Clear();
                        element.Clear();
                        indOfStr = 1;
                    }
                    continue;
                }

                // if string contains array
                // then bypass the whole block to the end
                if (currentString.Contains("["))
                {
                    //array element name
                    dataOfJSON.Append("   ");
                    dataOfJSON.Append("<");

                    while (currentString[indOfStr] != '"')
                    {
                        element.Append(currentString[indOfStr]);
                        indOfStr++;
                    }
                    dataOfJSON.Append(element);
                    dataOfJSON.Append(">");
                    dataOfJSONFile.Add(dataOfJSON.ToString());
                    dataOfJSON.Clear();

                    i++;
                    str = JSONInStrings[i];
                    //delete spaces from current string
                    currentString = builder.RemoveSpacesAtBeginning(str);
                    indOfStr      = 1;
                    nestedElement = element.Remove(element.Length - 1, 1);

                    // until we reach the end of the block
                    // prints elements of array into XML-file
                    while (currentString[indOfStr] != ']')
                    {
                        indOfStr = 1;
                        if (!currentString.Contains("{"))
                        {
                            dataOfJSON.Append("      ");
                            dataOfJSON.Append("<");

                            dataOfJSON.Append(nestedElement);
                            dataOfJSON.Append(">");

                            while (currentString[indOfStr] != '"')
                            {
                                value.Append(currentString[indOfStr]);
                                indOfStr++;
                            }

                            dataOfJSON.Append(value);

                            dataOfJSON.Append("</");
                            dataOfJSON.Append(nestedElement);
                            dataOfJSON.Append(">");

                            dataOfJSONFile.Add(dataOfJSON.ToString());
                            dataOfJSON.Clear();
                            value.Clear();
                        }
                        i++;
                        str = JSONInStrings[i];
                        //delete spaces from current string
                        currentString = builder.RemoveSpacesAtBeginning(str);
                        indOfStr      = 0;
                    }

                    // if we have reached the end of array in JSON-file
                    // then prints closing tag
                    if (currentString.StartsWith("]"))
                    {
                        dataOfJSON.Append("  ");
                        dataOfJSON.Append("</");
                        dataOfJSON.Append(element);
                        dataOfJSON.Append("s");
                        dataOfJSON.Append(">");

                        dataOfJSONFile.Add(dataOfJSON.ToString());
                        dataOfJSON.Clear();
                        element.Clear();
                        nestedElement.Clear();
                    }
                    indOfStr = 1;
                    continue;
                }

                // if string contains the end symbol of JSON-file
                // then print closing tag
                if (currentString.StartsWith("}"))
                {
                    dataOfJSON.Append("</object>");
                    dataOfJSONFile.Add(dataOfJSON.ToString());
                    dataOfJSON.Clear();
                }
                indOfStr = 1;
            }
            return(dataOfJSONFile);
        }