// writes request XML according to the parameter passed
        public void WriteRequest(string type, string searchValue, string mask)
        {
            // create a document object
            MSXML2.IXMLDOMDocument document = CreateDocument();

            // create request element
            MSXML2.IXMLDOMElement requestElem = document.createElement("request");

            // set attributes of request element
            requestElem.setAttribute("type", type);

            // if one of these kinds of request is to be made
            // specify the filename and pertaining info too.
            if (type.CompareTo("SHOWFILES") != 0)
            {
                string ReqType = "";
                if (type.CompareTo("CHAT") == 0)
                {
                    ReqType = "message";
                }
                else
                {
                    ReqType = "scope";
                }

                MSXML2.IXMLDOMElement file_infoElem = document.createElement(ReqType.ToString());

                if (type.CompareTo("CHAT") == 0)
                {
                    file_infoElem.setAttribute("sendername", searchValue.Substring(0, searchValue.IndexOf("(")));
                    file_infoElem.setAttribute("senderIP", searchValue.Substring(searchValue.IndexOf("(") + 1).Substring(0, searchValue.Substring(searchValue.IndexOf("(") + 1).Length - 1));
                    file_infoElem.setAttribute("chatmsg", mask);
                }
                else
                {
                    file_infoElem.setAttribute("type", searchValue);
                    file_infoElem.setAttribute("mask", mask);
                }
                requestElem.appendChild(file_infoElem);
            }

            // close and save the documdent
            SaveAndCloseDocument(requestElem, document);
        }
        // writes error XML responses
        public void WriteErrorResponse(string error)
        {
            // create a document object
            MSXML2.IXMLDOMDocument document = CreateDocument();
            // create response and error info elements
            MSXML2.IXMLDOMElement responseElem  = document.createElement("response");
            MSXML2.IXMLDOMElement errorInfoElem = document.createElement("errorinfo");

            // set attribute of response element
            responseElem.setAttribute("type", "ERROR");
            // set attribute of errorinfo element
            errorInfoElem.setAttribute("errorcode", "1");
            errorInfoElem.setAttribute("severity", "Error");
            errorInfoElem.setAttribute("description", error);
            // add errorinfo element to response object as a child
            responseElem.appendChild(errorInfoElem);
            // save the document
            SaveAndCloseDocument(responseElem, document);
        }
Example #3
0
        /// <summary>
        /// 給属性赋值
        /// </summary>
        /// <param name="nd">IXMLDOMElement节点</param>
        /// <param name="name">字段名</param>
        /// <param name="value">字段值</param>
        public static void setAttribute(MSXML2.IXMLDOMElement nd, string name, object value)
        {
            string strValue = "";

            if (value != null && value != DBNull.Value)
            {
                strValue = value.ToString();
            }
            nd.setAttribute(name, strValue);
        }
        private void WriteShowfileResponse(string reqType)
        {
            // create a document object
            MSXML2.IXMLDOMDocument document = CreateDocument();
            // create response and error info elements
            MSXML2.IXMLDOMElement responseElem = document.createElement("response");
            // set attribute of response element
            responseElem.setAttribute("type", reqType);

            // open share.ini for reading
            StreamReader readfile = new StreamReader(SharedResourceInfoPath);
            string       readData;

            // read entire file
            while ((readData = readfile.ReadLine()) != null)
            {
                try
                {
                    // for each entry in share .ini create a fileinfo element
                    // and fill it with required information
                    MSXML2.IXMLDOMElement file_infoElem = document.createElement("fileinfo");
                    int index = readData.IndexOf("=", 0);
                    file_infoElem.setAttribute("filename", readData.Substring(0, index));
                    file_infoElem.setAttribute("mask", readData.Substring(index + 1, 1));

                    int secindex = -1;
                    if (-1 != (secindex = readData.IndexOf("=", index + 1)))
                    {
                        file_infoElem.setAttribute("filesize", readData.Substring(secindex + 1));
                    }

                    // add this element to response element as child
                    responseElem.appendChild(file_infoElem);
                }
                catch (Exception e)      { MessageBox.Show("Problem faced while responding : " + e.Message); }
            }
            // close and save the documdent
            SaveAndCloseDocument(responseElem, document);
        }
        // responds for search requests
        private void WriteSearchResponse(MSXML2.IXMLDOMNode node)
        {
            try
            {
                MSXML2.IXMLDOMNode         scope      = node.firstChild;
                MSXML2.IXMLDOMNamedNodeMap nodemap    = scope.attributes;
                MSXML2.IXMLDOMNode         childNode  = nodemap.nextNode();
                MSXML2.IXMLDOMNode         childNode2 = nodemap.nextNode();
                string scopeVal = childNode.nodeValue.ToString();
                string maskVal  = childNode2.nodeValue.ToString();

                // make sure that search request has criteria specified in it
                if (0 != scope.nodeName.CompareTo("scope"))
                {
                    return;
                }

                // validated that directory's existing
                if (!Directory.Exists(scopeVal.Substring(0, scopeVal.LastIndexOf("\\") + 1)))
                {
                    throw new Exception("Directory does not exists any more");
                }

                MSXML2.IXMLDOMDocument document     = CreateDocument();
                MSXML2.IXMLDOMElement  responseElem = document.createElement("response");
                responseElem.setAttribute("type", "SHOWFILES");

                int i = 0;
                // get files in the specified directory satisfying the
                // given criteria
                string[] files = Directory.GetFiles(scopeVal.Substring(0, scopeVal.LastIndexOf("\\") + 1), scopeVal.Substring(scopeVal.LastIndexOf("\\") + 1));
                files.Initialize();

                while (i < files.Length)
                {
                    // make fileinfo elements and fill then up with
                    // required
                    MSXML2.IXMLDOMElement file_infoElem = document.createElement("fileinfo");
                    file_infoElem.setAttribute("filename", files[i]);
                    file_infoElem.setAttribute("mask", maskVal);
                    file_infoElem.setAttribute("filesize", Convert.ToString(new FileInfo(files[i]).Length));
                    ++i;
                    // add them to response element as children;
                    responseElem.appendChild(file_infoElem);
                }

                // get files in the specified directory satisfying the
                // given criteria
                string[] dirs = Directory.GetDirectories(scopeVal.Substring(0, scopeVal.LastIndexOf("\\") + 1), scopeVal.Substring(scopeVal.LastIndexOf("\\") + 1));
                dirs.Initialize();

                i = 0;
                while (i < dirs.Length)
                {
                    // make fileinfo elements and fill then up with
                    // required
                    MSXML2.IXMLDOMElement file_infoElem = document.createElement("fileinfo");
                    file_infoElem.setAttribute("filename", dirs[i] + "\\");
                    file_infoElem.setAttribute("mask", maskVal);
                    ++i;

                    // add them to response element as children;
                    responseElem.appendChild(file_infoElem);
                }
                // close and save the document
                SaveAndCloseDocument(responseElem, document);
            }
            catch (Exception e) { WriteErrorResponse(e.Message); }
        }