Exemplo n.º 1
0
        /// <summary>
        /// This is similar to GetStringWithXPath, but the path argument identifies a
        /// single root node, which contains string elemenents (and optionally others,
        /// which are ignored), each containing id and optionally txt elements.
        /// This is much more efficient than GetStringWithXPath when multiple items
        /// are wanted from the same root node, but only reliable when the path
        /// identifies a single root.
        /// </summary>
        /// <param name="id"></param>
        /// <param name="rootXPathFragment"></param>
        /// <param name="fTrim"></param>
        /// <returns></returns>
        private string GetStringWithRootXPath(string id, string rootXPathFragment, bool fTrim)
        {
            if (fTrim)
            {
                id = id.Trim();
            }
            Dictionary <string, string> items = null;

            if (m_pathsToStrings.ContainsKey(rootXPathFragment))
            {
                items = m_pathsToStrings[rootXPathFragment];
            }
            else
            {
                string path = "strings/" + rootXPathFragment;
                if (path[path.Length - 1] == '/')
                {
                    path = path.Substring(0, path.Length - 1);                     // strip closing slash.
                }
                XmlNode parent = m_document.SelectSingleNode(path);
                items = new Dictionary <string, string>();
                m_pathsToStrings[rootXPathFragment] = items;
                if (parent != null)
                {
                    foreach (XmlNode child in parent.ChildNodes)
                    {
                        string idChild = XmlUtils.GetOptionalAttributeValue(child, "id");
                        //if the txt attribute is missing, use the id attr,
                        //as this is unacceptable shorthand to use for English entries.
                        //e.g.: <string id="Anywhere"/> is equivalent to <string id="Anywhere" txt="Anywhere"/>
                        string txt = XmlUtils.GetOptionalAttributeValue(child, "txt", idChild);
                        if (child.Name == "string" && idChild != null)
                        {
                            items[idChild] = txt;
                        }
                    }
                }
            }
            if (items.ContainsKey(id))
            {
                return(items[id]);
            }

            if (m_parent != null)
            {
                return(m_parent.GetStringWithXPath(id, rootXPathFragment));
            }
            return("*" + id + "*");
        }
Exemplo n.º 2
0
 public void WithXPathFragment()
 {
     //find any group that contains a match
     //the leading '/' here will lead to a double slash,
     //	something like strings//group,
     //meaning that this can be found in any group.
     Assert.AreEqual(m_table.GetStringWithXPath("MyPineapple", "/group/"), "pnppl");
 }