Exemplo n.º 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);
        }
Exemplo n.º 2
0
        public bool TransformString(string sourceString, ref string targetString, XSLTExtensionTypes extType, string additionalString)
        {
            try
            {
                StringBuilder sb = new StringBuilder();
                using (StringWriter sw = new StringWriter(sb))
                {
                    using (StringReader sr = new StringReader(sourceString))
                    {
                        using (XmlTextReader xtr = new XmlTextReader(sr))
                        {
                            using (XmlTextWriter stw = new XmlTextWriter(sw))
                            {
                                stw.Formatting = Formatting.Indented;

                                XsltArgumentList arg = XSLTExtension.GetXsltArgumentList(extType, sourceString, additionalString);
                                bool             res = TransformXmlWithExtension(xtr, stw, arg);

                                if (res)
                                {
                                    targetString = sb.ToString();
                                }
                                return(res);
                            }
                        }
                    }
                }
            }
            catch (Exception err)
            {
                NotifyError(err);
                return(false);
            }
        }
Exemplo n.º 3
0
 public string GetValue(string xpath, string prefixes)
 {
     if (xpath == null || xpath.Length < 1)
     {
         return("");
     }
     return(XSLTExtension.GetValueWithXPath(_xmlString, xpath, prefixes));
 }
Exemplo n.º 4
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)";
            //}
        }