Advertisement

01.18.2008 at 10:35AM PST, ID: 23094204
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.2

Web Service and VBScript

Asked by tommychiu in VB Script, C# Programming Language

Tags: ,

I can create the object but it fails on the ValidateXML method.

The error message: "XMLValidation: Object reference not set to an instance of an object.

The XMLValidation code and the vbscript provided as follow.Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
305:
306:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Text;
using System.Runtime.Serialization;
using System.Runtime.InteropServices;
using System.Data.SqlClient;
 
namespace Thomson.TTA.DataExchange.XMLValidation
{    
    #region COM interop interface definition
	/// <summary>
	/// COM interop interface definition for accessing the RSTaxServices methods via
	/// COM languages such as VBScript.  See the <see cref="RSTaxWSInterface"/> class
	/// for complete descriptions of the interface elements.
	/// </summary>
	// Since the .NET Framework interface and coclass have to behave as 
	// COM objects, we have to give them guids.
    [Guid("AB439215-C3A8-4e28-B382-9866DD27D6C3")]
	[InterfaceType(ComInterfaceType.InterfaceIsDual)]
	public interface IXMLValidatorInterface
	{
        [DispId(1)]
        bool ValidateXML(string strDCAppl, string strImportExport, string strFirm, string strAccount, string strTaxAppl, string strTaxType, string strLocator, string strYear, string strXMLFile, string strXSDFile, out string ErrCode);		
	}
	#endregion	
	
	/// <summary>
    /// Summary description for XMLValidation
    /// </summary>
    [Guid("4C79CEF3-0F73-4c49-ABF0-61E2837529F4")]
    [ClassInterface(ClassInterfaceType.None)]
    [WebService(Namespace="Thomson.TTA.DataExchange.XMLValidation", Description="Data Exchange XML Validation")]
    public class XMLValidation : System.Web.Services.WebService, IXMLValidatorInterface, IDisposable
    {
        private bool XmlIsNotValid = false;
        private bool bolExceptions = false;
        
        MemoryStream oStream = null;
        
        System.Xml.XmlTextWriter xWriter = null;
        
        private string LogDirPath = "";
 
        //This method validates an xml file.
       [WebMethod(MessageName = "ValidateXML", Description = "This method validates the XML file.")]
       public bool ValidateXML(string strDCAppl, string strImportExport, string strFirm, string strAccount, string strTaxAppl, string strTaxType, string strLocator, string strYear, string strXMLFile, string strXSDFile, out string ErrCode)
        {
            bool retVal = false;
 
            ErrCode = "";
 
            if (File.Exists(@strXMLFile))
            {
                StreamReader sr = new StreamReader(@strXMLFile);
                string xmlString = sr.ReadToEnd();
 
                if (xmlString != "")
                {
                    try
                    {
                        LogDirPath = GetDataRetrievalPath();
 
                        if (LogDirPath == "")
                        {
                            ErrCode = "DATA_RETRIEVE_MGT_ERROR";
                            return false;
                        }
 
                        LogDirPath += "\\" + strDCAppl + strImportExport + strFirm + strAccount + strTaxAppl + strTaxType + strLocator + strYear;
 
                        Directory.CreateDirectory(LogDirPath);
 
                        //Start recording
                        oStream = new MemoryStream();
                        xWriter = new System.Xml.XmlTextWriter(oStream, System.Text.Encoding.UTF8);
                        xWriter.Formatting = System.Xml.Formatting.Indented;
                        xWriter.WriteStartDocument(true);
                        xWriter.WriteStartElement(strDCAppl + strImportExport);
                        xWriter.WriteAttributeString("file", strXMLFile);
 
                        retVal = ValidateXmlFragment(xmlString, GetBaseDirectory() + strXSDFile);
 
                        xWriter.WriteEndDocument();
 
                        if (bolExceptions == true)
                        {
                            xWriter.Flush();
                            oStream.Position = 0;
                            StreamReader srDoc = new StreamReader(oStream);
                            XmlDocument xLogDoc = new XmlDocument();
                            xLogDoc.LoadXml(srDoc.ReadToEnd());
                            xLogDoc.Save(LogDirPath + "\\" + strDCAppl + "_ERROR_LOG.xml");
 
                            //Copy source file too
                            File.Copy(strXMLFile, LogDirPath + "\\" + Path.GetFileName(strXMLFile), true);
 
                            //Now delete file
                            File.Delete(strXMLFile);
                        }
                        else
                        {
                            try
                            {
                                //Delete log dir
                                Directory.Delete(LogDirPath);
                            }
                            catch (Exception)
                            {
                                ErrCode = "ERROR_DELETING_FOLDER";
                            }
                        }
                    }
                    catch (Exception e)
                    {
                        throw e;
                    }
                    finally
                    {
                        sr.Close();
                    }
                }
                else
                {
                    ErrCode = "INVALID_FILE";
                    return false;
                }
            }
            else
            {
                ErrCode = "INVALID_FILE";
                return false;
            }
            return retVal;
        }
 
        private static string GetBaseDirectory()
        {
            string BaseDirectory = System.Reflection.Assembly.GetExecutingAssembly().Location;
            BaseDirectory = Path.GetDirectoryName(BaseDirectory);
 
            if (BaseDirectory.EndsWith("\\"))
                return BaseDirectory;
            else
                return BaseDirectory + "\\";
        }
 
        private bool ValidateXmlFragment(string fragment, string schemaPath)
        {
            XmlReader reader = null;
       
            try
            {
                ValidationEventHandler veh = new ValidationEventHandler(ValidationHandler);
                XmlSchema schema = XmlSchema.Read(new XmlTextReader(schemaPath), veh);
 
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.Schemas.Add(schema);
                settings.IgnoreWhitespace = true;
                settings.ValidationType = ValidationType.Schema; ////this line is neccessary for validation to work
                settings.ValidationEventHandler += new ValidationEventHandler(ValidationHandler);
                reader = XmlReader.Create(new StringReader(fragment), settings);
    
                while (reader.Read());
                reader.Close();
 
                if (bolExceptions == true)
                    xWriter.WriteEndElement(); // </Exceptions>
 
                xWriter.WriteEndElement(); // </strDCAppl + strImportExport>
 
                if (XmlIsNotValid)
                    return false;
 
                return true;
            }
            catch (Exception ex)
            {
                WriteValidationError(ex.Message);
                bolExceptions = true;
                xWriter.WriteEndElement(); // </Exceptions>
                xWriter.WriteEndElement(); // </strDCAppl + strImportExport>
                return false;
            }
            finally
            {
                reader.Close();
            }
        }
 
        private void ValidationHandler(object sender, ValidationEventArgs args)
        {
            XmlIsNotValid = true;
            WriteValidationError(args.Message.ToString());
            bolExceptions = true;
        }
 
        private void WriteValidationError(string err)
        {
            if (xWriter != null)
            {
                if (bolExceptions == false)
                    xWriter.WriteStartElement("ValidationErrors");
                    xWriter.WriteStartElement("ValidationError");
                    xWriter.WriteString(err);
                    xWriter.WriteEndElement();
                    //Remember to set bolExceptions = true; when you exit back out to main routine.
            }
        }
 
        private static string GetDataRetrievalPath()
        {
            string sServer = "";
            string sPort = "";
            string sDatabase = "";
            string sUser = "";
            string sPwd = "";
            string strSQl = "";
 
            DataAccess oManagerData = null;
 
            try
            {
                Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(@"SOFTWARE\CLR\RS.Installation\eDatabases\eRSDataSource");
 
                if (regkey == null)
                    throw new Exception("Error obtaining credentials");
                else
                {
                    sServer = MTIDataObjects.Thomson.Ria.InSource.GlobalDataCollect.Crypto.RSDecrypt((string)regkey.GetValue("Server"));
                    sPort = MTIDataObjects.Thomson.Ria.InSource.GlobalDataCollect.Crypto.RSDecrypt((string)regkey.GetValue("Port"));
                    sDatabase = MTIDataObjects.Thomson.Ria.InSource.GlobalDataCollect.Crypto.RSDecrypt((string)regkey.GetValue("Database"));
                    sUser = MTIDataObjects.Thomson.Ria.InSource.GlobalDataCollect.Crypto.RSDecrypt((string)regkey.GetValue("UID"));
                    sPwd = MTIDataObjects.Thomson.Ria.InSource.GlobalDataCollect.Crypto.RSDecrypt((string)regkey.GetValue("PWD"));
                    string ConnStrMan = "Server=" + sServer + "," + sPort + ";User ID=" + sUser + ";Password=" + sPwd + ";Initial Catalog=" + sDatabase;
 
                    strSQl = "select value from WebConfig(nolock) where parameter = 'DataRetrievePath'";
                    oManagerData = new DataAccess(ConnStrMan);
                    SqlDataReader drManager = oManagerData.GetData(strSQl);
 
                    if (drManager.HasRows)
                    {
                        drManager.Read();
                        return drManager.GetValue(0).ToString();
                    }
                    return "";
                }
            }
            catch (Exception)
            {
                return "";
            }
            finally
            {
                oManagerData.Close();
            }
        }
    }
}
*******************************VBScript
'<SCRIPT LANGUAGE="VBScript">
 
 
Main
 
 
Sub Main
 
 
    Dim objValidator
    set objValidator = CreateObject("Thomson.TTA.DataExchange.XMLValidation.XMLValidation")
 
    If Err.Number <> 0 Or Not IsObject(objValidator) Then
        MsgBox "Unable to create XMLValidator", 100, Err.Description
    else
          MsgBox "XMLValiator Successfully created", 100, "XMLValiator Successfully created"
    end if	
 
    dim bRetVal
    dim strErrorCode
    strErrorCode = ""
    
    bRetVal = objValidator.ValidateXML("TEQ", "IMP", "FT", "7232", "1040", "A", "98784C", "2007", "C:\ftdata\2007\TEQ\A98784C7.xml", "C:\datadict\2007\appfiles\teq\tta1040teq7schema.xsd", strErrorCode)
    
    If bRetVal Then
        MsgBox "bRetVal XMLValidator Error", 100, Err.Description
    else
        MsgBox "bRetVal XMLValidator OK", 100, Err.Description    
    end if   
    
    If Err.Number <> 0 then 
        MsgBox "Err XMLValidator Error", 100, Err.Description
    else
        MsgBox "XMLValidator OK", 100, Err.Description
    end if   
    
End Sub
 
'</SCRIPT>
[+][-]01.21.2008 at 05:17AM PST, ID: 20705723

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 06:30PM PST, ID: 20711494

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 09:07PM PST, ID: 20712030

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 09:20PM PST, ID: 20712069

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.21.2008 at 10:01PM PST, ID: 20712209

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]01.23.2008 at 07:12AM PST, ID: 20723867

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]01.23.2008 at 02:33PM PST, ID: 20728642

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: VB Script, C# Programming Language
Tags: Visual Studio, 2005
Sign Up Now!
Solution Provided By: tommychiu
Participating Experts: 2
Solution Grade: B
 
 
[+][-]01.24.2008 at 09:56AM PST, ID: 20735401

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628