예제 #1
0
 public void ReadXml(XmlReader reader)
 {
     if (reader.IsStartElement())
     {
         while (reader.Read())
         {
             if (reader.Name == "item")
             {
                 XmlSerializer  xmlSerializer = new XmlSerializer(typeof(AXmlPluralItem));
                 AXmlPluralItem item          = (AXmlPluralItem)xmlSerializer.Deserialize(reader);
                 this.Items.Add(item.Quantity, item);
             }
         }
     }
 }
예제 #2
0
 public void Add(AXmlPluralItem item)
 {
     this.Items.Add(item.Quantity, item);
 }
예제 #3
0
        public static AXmlResource ReadAXml(string path)
        {
            AXmlResource  res      = null;
            DirectoryInfo dir      = new DirectoryInfo(Path.GetDirectoryName(path));
            string        language = "en";

            if (dir.Name.Contains("-"))
            {
                language = dir.Name.Substring(dir.Name.IndexOf("-") + 1);
            }

            using (XmlReader reader = XmlReader.Create(path, new XmlReaderSettings()
            {
                IgnoreComments = false
            }))
            {
                res = new AXmlResource();
                AXmlPlural    xmlPlural       = null;
                bool          resourceContent = false;
                List <string> comments        = new List <string>();
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Comment)
                    {
                        comments.Add(reader.Value.Trim());
                        continue;
                    }
                    if (reader.IsStartElement())
                    {
                        if (reader.Name == N_RESOURCE)
                        {
                            resourceContent = true;
                            continue;
                        }
                    }
                    if (!resourceContent)
                    {
                        continue;
                    }

                    if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (reader.Name == N_PLURALS)
                        {
                            xmlPlural = null;
                            continue;
                        }
                        else if (reader.Name == N_RESOURCE)
                        {
                            resourceContent = false;
                            continue;
                        }
                    }

                    if (reader.IsStartElement())
                    {
                        if (reader.Name == N_STRING)
                        {
                            AXmlString xmlString = new AXmlString();
                            xmlString.Name = reader.GetAttribute(A_NAME);
                            if (comments.Count > 0)
                            {
                                xmlString.Comments.AddRange(comments);
                                comments.Clear();
                            }
                            string trans = reader.GetAttribute(A_TRANSLATABLE);
                            if (!string.IsNullOrEmpty(trans))
                            {
                                xmlString.IsTranslatable = Convert.ToBoolean(trans);
                            }
                            xmlString.Value = reader.ReadElementContentAsString();
                            res.Add(xmlString);
                            continue;
                        }
                        else if (reader.Name == N_PLURALS)
                        {
                            xmlPlural      = new AXmlPlural();
                            xmlPlural.Name = reader.GetAttribute(A_NAME);
                            if (comments.Count > 0)
                            {
                                xmlPlural.Comments.AddRange(comments);
                                comments.Clear();
                            }
                            res.Add(xmlPlural);
                            continue;
                        }
                        else if (reader.Name == N_ITEM)
                        {
                            if (xmlPlural == null)
                            {
                                continue;
                            }

                            AXmlPluralItem item = new AXmlPluralItem();
                            item.Quantity = (QuantityType)Enum.Parse(typeof(QuantityType), reader.GetAttribute(A_QUANTITY));
                            item.Value    = reader.ReadElementContentAsString();

                            xmlPlural.Add(item);
                        }
                    }
                }
            }

            if (res != null)
            {
                res.Language = language;
            }

            return(res);
        }