Example #1
0
        private string getValueFromXml(XDocument doc, XmlPath path)
        {
            string result = "";

            try
            {
                XElement step1 = path.Pop().GetAtom();;
                IEnumerable <XElement> try1 = doc.Elements(step1.Name);

                while (path.Count() > 0)
                {
                    step1 = path.Pop().GetAtom();
                    try1  = try1.Elements(step1.Name);
                    if (step1.HasAttributes)
                    {
                        try1 = try1.Where(x => (string)x.Attribute("name") == step1.Attribute("name").Value.ToString()).ToArray();
                    }
                    else
                    {
                        try1 = try1.Where(x => x.HasAttributes == false).ToArray();
                    }
                    //try1 = try1.Where(x => x.Attributes().Count() == 0).ToArray();
                }

                result = try1.First().Value.ToString();
            }
            catch
            {
                result = "<NO DATA>";
            }
            result = result.Trim();
            return(result);
        }
        private void parseXmlFile(string language, string filename)
        {
            XmlPath   myPath = new XmlPath();
            string    key    = "";
            bool      gotten = false;
            string    text   = "";
            XmlReader reader = new XmlTextReader(filename);

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:     // Узел является элементом.
                    String tempAttr = (reader.HasAttributes) ? (reader.GetAttribute(0)) : ("");
                    key = tempAttr;
                    myPath.Push(new PathAtom(reader.Name, tempAttr));
                    if (reader.IsEmptyElement)
                    {
                        if (xmlDict.ContainsKey(myPath.GetPathAsString()))
                        {
                            xmlDict[myPath.GetPathAsString()].SetTranslation(language, text);
                        }
                        else
                        {
                            xmlDict.Add(myPath.GetPathAsString(), new CMultiData(key, language, text, filename, myPath));
                        }
                        text = "";
                        myPath.Pop();
                    }
                    break;

                case XmlNodeType.Text:     // Нашли текст в элементе, сохраняем его
                    text   = reader.Value.Trim();
                    gotten = true;
                    break;

                case XmlNodeType.EndElement:     // Нашли конец элемента, сохраняем данные в словарь
                    if (gotten)
                    {
                        if (xmlDict.ContainsKey(myPath.GetPathAsString()))
                        {
                            xmlDict[myPath.GetPathAsString()].SetTranslation(language, text);
                        }
                        else
                        {
                            xmlDict.Add(myPath.GetPathAsString(), new CMultiData(key, language, text, filename, myPath));
                        }
                        gotten = false;
                        text   = "";
                        key    = "";
                    }
                    myPath.Pop();
                    break;
                }
            }
        }
Example #3
0
        private void parseXmlFile(XmlReader reader, XDocument engDoc, string filename)
        {
            string  phrase = "";
            string  eng    = "";
            XmlPath myPath = new XmlPath();
            bool    gotten = false;

            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                case XmlNodeType.Element:     // Узел является элементом.
                    String tempAttr = (reader.HasAttributes) ? (reader.GetAttribute(0)) : ("");
                    myPath.Push(new PathAtom(reader.Name, tempAttr));
                    if (reader.IsEmptyElement)
                    {
                        eng = "";
                        xmlDict.Add(id++, new CXmlData(phrase, eng, filename, myPath));
                        phrase = "";
                        eng    = "";
                        myPath.Pop();
                    }
                    break;

                case XmlNodeType.Text:     // Нашли текст в элементе, сохраняем его
                    phrase = reader.Value.Trim();
                    gotten = true;
                    break;

                case XmlNodeType.EndElement:     // Нашли конец элемента, сохраняем данные в словарь
                    if (gotten)
                    {
                        XmlPath cpy = new XmlPath(myPath);
                        eng = getValueFromXml(engDoc, cpy);
                        xmlDict.Add(id++, new CXmlData(phrase, eng, filename, myPath));
                        gotten = false;
                        phrase = "";
                        eng    = "";
                    }
                    myPath.Pop();
                    break;
                }
            }
        }