Example #1
0
        public string EvaluateXmlDocument(string xcmd)
        {
            WriteLine("EvaluateXmlDocument :" + xcmd);

            string response = "<request>\r\n <cmd>" + xcmd + "</cmd>\r\n <response>null</response>\r\n</request>";

            try
            {
                if (xcmd.Contains(".xlsp"))
                {
                    return(XML2Lisp(xcmd));
                }


                int           depth = 0;
                var           xdoc  = new XmlDocumentLineInfo();
                XmlTextReader reader;
                StringReader  stringReader;
                if (xcmd.Contains("http:") || xcmd.Contains(".xml"))
                {
                    // assuming its a file
                    xcmd   = xcmd.Trim();
                    reader = XmlDocumentLineInfo.CreateXmlTextFileReader(xcmd);
                    xdoc.Load(xcmd);
                }
                else
                {
                    // otherwise just use the string
                    stringReader = new System.IO.StringReader(xcmd);
                    reader       = new XmlTextReader(stringReader);
                    xdoc.LoadXml(xcmd);
                }

                Hashtable[] attributeStack = new Hashtable[16];


                string[] strURI  = new String[16];
                string[] strName = new String[16];
                string[] strPath = new String[16];

                string totalResponse = "";
                for (int i = 0; i < 16; i++)
                {
                    attributeStack[i] = new Hashtable();
                }

                while (reader.Read())
                {
                    depth = reader.Depth + 1;
                    switch (reader.NodeType)
                    {
                    case XmlNodeType.Element:
                        //Hashtable attributes = new Hashtable();
                        strURI[depth]  = reader.NamespaceURI;
                        strName[depth] = reader.Name;
                        strPath[depth] = strPath[depth - 1] + "." + strName[depth];
                        if (reader.HasAttributes)
                        {
                            for (int i = 0; i < reader.AttributeCount; i++)
                            {
                                reader.MoveToAttribute(i);
                                string attributeName  = reader.Name;
                                string attributeValue = reader.Value;
                                string attributePath  = "";
                                if ((attributeName == "name") && ((strName[depth] == "param") || (strName[depth] == "feeling")))
                                {
                                    // so you can have multiple named params
                                    strPath[depth] = strPath[depth] + "." + attributeValue;
                                }
                                if (depth > 1)
                                {
                                    attributePath = strPath[depth] + "." + attributeName;
                                }
                                else
                                {
                                    attributePath = attributeName;
                                }
                                overwrite2Hash(attributeStack[depth], attributeName, attributeValue);
                                // zero depth contains the fully qualified nested dotted value
                                // i.e. pet-action-plan.action.param.vector.x
                                // i.e. pet-action-plan.action.param.entity.value
                                overwrite2Hash(attributeStack[0], attributePath, attributeValue);
                            }
                        }
                        overwrite2Hash(attributeStack[depth], "ElementName", strName[depth]);
                        overwrite2Hash(attributeStack[depth], "Path", strPath[depth]);
                        xStartElement(strURI[depth], strName[depth], attributeStack[depth], depth, attributeStack);
                        if (reader.IsEmptyElement)
                        {
                            // do whatever EndElement would do
                            response       = xEndElement(strURI[depth], strName[depth], attributeStack[depth], depth, attributeStack);
                            totalResponse += response + "\r\n";
                        }
                        break;
                    //
                    //you can handle other cases here
                    //

                    case XmlNodeType.Text:
                        // Todo
                        WriteLine(" TextNode: depth=" + depth.ToString() + "  path = " + strPath[depth - 1]);;
                        if (reader.Name == "param")
                        {
                            overwrite2Hash(attributeStack[depth], strPath[depth - 1] + ".param." + strName[depth] + ".InnerText", reader.Value);
                            overwrite2Hash(attributeStack[0], strPath[depth - 1] + ".param." + strName[depth] + ".InnerText", reader.Value);
                        }
                        else
                        {
                            overwrite2Hash(attributeStack[depth], strPath[depth - 1] + ".InnerText", reader.Value);
                            overwrite2Hash(attributeStack[0], strPath[depth - 1] + ".InnerText", reader.Value);
                        }
                        break;

                    case XmlNodeType.EndElement:
                        response       = xEndElement(strURI[depth], strName[depth], attributeStack[depth], depth, attributeStack);
                        totalResponse += response + "\r\n";
                        // Todo
                        //depth--;
                        break;

                    default:
                        break;
                    } //switch
                }     //while
                string finalResponse = "<pet-petaverse-msg>\r\n" + totalResponse + "</pet-petaverse-msg>\r\n";
                return(finalResponse);
            } //try
            catch (Exception e)
            {
                WriteLine("error occured: " + e.Message);
                WriteLine("        Stack: " + e.StackTrace.ToString());
                return("<error><response>" + response + "</response><errormsg>" + e.Message.ToString() + "</errormsg> </error>");
            }
        }