示例#1
0
        private XmlNode FindXmlNode(string xpath, string prefixes)
        {
            XmlNode node = null;

            if (prefixes == null || prefixes.Length < 1)
            {
                node = _doc.SelectSingleNode(xpath);
            }
            else
            {
                XmlNamespaceManager nsm = new XmlNamespaceManager(_doc.NameTable);
                XSLTExtension.FillXmlNamespaceManager(nsm, prefixes);

                //int i = 0;
                //string[] prefixList = prefixes.Split('|');
                //while (i < prefixList.Length)
                //{
                //    string prefix = prefixList[i].Trim();
                //    if (++i >= prefixList.Length) break;
                //    string nsURI = prefixList[i++].Trim();
                //    nsm.AddNamespace(prefix, nsURI);
                //}

                node = _doc.SelectSingleNode(xpath, nsm);
            }

            return(node);
        }
示例#2
0
        internal static string GetValueWithXPath(string xmlString, string xpath, string prefixDef)
        {
            if (xpath == null || xpath.Length < 1)
            {
                return("");
            }
            if (xmlString == null || xmlString.Length < 1)
            {
                return("");
            }

            //try
            //{
            using (StringReader sr = new StringReader(xmlString))
            {
                XPathDocument  doc = new XPathDocument(sr);
                XPathNavigator nvg = doc.CreateNavigator();

                XmlNamespaceManager nsMgr = null;
                if (prefixDef != null && prefixDef.Length > 0)
                {
                    nsMgr = new XmlNamespaceManager(nvg.NameTable);
                    XSLTExtension.FillXmlNamespaceManager(nsMgr, prefixDef);
                }

                object o     = (nsMgr == null) ? nvg.Evaluate(xpath) : nvg.Evaluate(xpath, nsMgr);
                string value = (o != null) ? o.ToString() : "";         //"(null)";

                XPathNodeIterator i = o as XPathNodeIterator;
                if (i != null)
                {
                    StringBuilder sb = new StringBuilder();
                    while (i.MoveNext())
                    {
                        sb.Append(i.Current.Value); //.Append(';');
                    }
                    value = sb.ToString();          //.TrimEnd(';');
                }

                return(value);
            }
            //}
            //catch (Exception e)
            //{
            //    Program.Log.Write(e);
            //    return "";  //"(error)";
            //}
        }