Click here to Skip to main content
15,888,521 members
Home / Discussions / C#
   

C#

 
AnswerRe: Problem adding data from a data set to a List Object Pin
Dave Kreskowiak1-Oct-09 11:15
mveDave Kreskowiak1-Oct-09 11:15 
QuestionHow do you check to see if a web service is running or not? Pin
AndyASPVB1-Oct-09 10:49
AndyASPVB1-Oct-09 10:49 
AnswerRe: How do you check to see if a web service is running or not? Pin
Dave Kreskowiak1-Oct-09 11:14
mveDave Kreskowiak1-Oct-09 11:14 
AnswerRe: How do you check to see if a web service is running or not? Pin
PIEBALDconsult1-Oct-09 14:55
mvePIEBALDconsult1-Oct-09 14:55 
GeneralRe: How do you check to see if a web service is running or not? Pin
Dave Kreskowiak1-Oct-09 16:23
mveDave Kreskowiak1-Oct-09 16:23 
GeneralRe: How do you check to see if a web service is running or not? Pin
PIEBALDconsult2-Oct-09 3:42
mvePIEBALDconsult2-Oct-09 3:42 
QuestionException while creating table in azure. Pin
VasntKrish1-Oct-09 9:23
VasntKrish1-Oct-09 9:23 
QuestionXML, XSD VALIDATION Pin
waqasm1-Oct-09 8:16
waqasm1-Oct-09 8:16 
Hello Everyone, Im an extremely new programmer and this is the first time im trying out something like this.

I need to upload a XSD file and a XML file, then I need to check whether the XML file is valid comparing to the XSD file. These files are on my desktop.

Here is my code so far, but I am missing a lot of things. Any help will be great.

Button1: This is my validate Button
Button 2: This is my XML upload Button
Button 3: This is my XSD validate Button
inputtext: This is my textbox where the xml file location
inputtext2: This is my textbox where the xsd file location
textbox1: this is my output button.

One of the main things im having trouble with is that once my file link shows up to the inputtext, I dont know how to run the validation check on it with the xsd file. I know my code doesnt have any link with the xml and xsd files...its becasue I dont know how to link them. You may assume file names to be sample.xml and sample.xsd. Thanks

using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.IO;
using System.Xml;
using System.Xml.Schema;


namespace ValidateXML
{

public partial class Form1 : Form
{
public Form1()
{

InitializeComponent();

}

string strXML; //- In put XML file
string strXSD; //- In put XSD file
string strXSDNS;

void ValidateXML(string strXML, string strXSD, string strXSDNS)
{
//Create a XmlValidatingReader, XmlSchemaCollection and ValidationEventHandler objects to be used to validate the XML against an XSD file

XmlValidatingReader reader = null;
XmlSchemaCollection myschema = new XmlSchemaCollection();
ValidationEventHandler eventHandler = new ValidationEventHandler(ShowCompileErrors);

try
{
//Create the XML fragment to be parsed.

XmlDocument doc = new XmlDocument();
doc.Load(strXML);
string xmlFrag = doc.InnerXml;

//Create an XmlParserContext object for use with the XMLValidatingReader:

/////String xmlFrag = "<author xmlns='urn:bookstore-schema' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>" +
// "<first-name>Herman</first-name>" +
// "<last-name>Melville</last-name>" +
//"</author>";

//Create the XmlParserContext.
XmlParserContext context = new XmlParserContext(null, null, "", XmlSpace.None);

//Implement the reader.
reader = new XmlValidatingReader(xmlFrag, XmlNodeType.Element, context);

// Add the relevant schema files (.XSD) to the name space we are checking
//against and repeat until all the schema's have an associated XSD file with them.

//Add the schema.
myschema.Add(strXSD, strXSD);

//Set the schema type and add the schema to the reader.
reader.ValidationType = ValidationType.Schema;
reader.Schemas.Add(myschema);
// Read in the XML Data:

while (reader.Read())
{

}

//If there is no exception, display "Completed Validating" or return a value indicating success

textBox1.Text = "Completed validating " + strXML;
}
//Watch out for exceptions within the XML file and display them appropriately to the user:

catch (XmlException XmlExp)
{
textBox1.Text = "XMLException " + XmlExp.Message;
}

//XMLSchemaExceptions are thrown when the XML document does not match the schema provided.

catch (XmlSchemaException XmlSchExp)
{
textBox1.Text = "XMLSchemaException " + XmlSchExp.Message;
}

//Catch all other exceptions and report them to the user:

catch (Exception GenExp)
{
textBox1.Text = "Exception " + GenExp.Message;
}

}

private void button1_Click(object sender, EventArgs e)
{
if (inputtext.Text == String.Empty)
{
textBox1.Text = ("Warning: Upload a XML file");
}
else if (inputtext2.Text == String.Empty)
{
textBox1.Text = ("Warning: Upload a XSD File");
}

else
{
ValidateXML(strXML, strXSD, "urn:bookstore-schema");

}
}

private void button2_Click(object sender, EventArgs e)
{

// Create an OpenFileDialog object.
OpenFileDialog openFile1 = new OpenFileDialog();

// Initialize the OpenFileDialog to look for text files.
openFile1.Filter = "(*.xml)|*.xml";

if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
inputtext.Text = openFile1.FileName;
}
}

private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog openFile1 = new OpenFileDialog();
openFile1.Filter = "(*.xsd)|*.xsd";
if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
inputtext2.Text = openFile1.FileName;
}


}
}

Thanks in advance for anyone that has helped me.
AnswerRe: XML, XSD VALIDATION Pin
Henry Minute1-Oct-09 10:50
Henry Minute1-Oct-09 10:50 
GeneralRe: XML, XSD VALIDATION Pin
waqasm1-Oct-09 11:09
waqasm1-Oct-09 11:09 
GeneralRe: XML, XSD VALIDATION Pin
Henry Minute1-Oct-09 11:48
Henry Minute1-Oct-09 11:48 
GeneralRe: XML, XSD VALIDATION Pin
waqasm2-Oct-09 3:56
waqasm2-Oct-09 3:56 
GeneralRe: XML, XSD VALIDATION Pin
Henry Minute2-Oct-09 5:29
Henry Minute2-Oct-09 5:29 
GeneralRe: XML, XSD VALIDATION Pin
waqasm7-Oct-09 6:14
waqasm7-Oct-09 6:14 
QuestionSqlServerCeException "There was an error parsing the query" help .... Pin
hande541-Oct-09 6:22
hande541-Oct-09 6:22 
AnswerRe: SqlServerCeException "There was an error parsing the query" help .... Pin
Md. Marufuzzaman1-Oct-09 7:07
professionalMd. Marufuzzaman1-Oct-09 7:07 
GeneralRe: SqlServerCeException "There was an error parsing the query" help .... Pin
hande541-Oct-09 7:57
hande541-Oct-09 7:57 
GeneralRe: SqlServerCeException "There was an error parsing the query" help .... Pin
Md. Marufuzzaman1-Oct-09 8:29
professionalMd. Marufuzzaman1-Oct-09 8:29 
GeneralRe: SqlServerCeException "There was an error parsing the query" help .... Pin
hande541-Oct-09 8:50
hande541-Oct-09 8:50 
GeneralRe: SqlServerCeException "There was an error parsing the query" help .... Pin
Abhishek Sur1-Oct-09 10:31
professionalAbhishek Sur1-Oct-09 10:31 
GeneralRe: SqlServerCeException "There was an error parsing the query" help .... Pin
hande541-Oct-09 10:33
hande541-Oct-09 10:33 
GeneralRe: SqlServerCeException "There was an error parsing the query" help .... Pin
Md. Marufuzzaman1-Oct-09 20:15
professionalMd. Marufuzzaman1-Oct-09 20:15 
Questionsocket problem: works in visual studio, not as release Pin
TimWallace1-Oct-09 5:49
TimWallace1-Oct-09 5:49 
AnswerRe: socket problem: works in visual studio, not as release Pin
Luc Pattyn1-Oct-09 6:22
sitebuilderLuc Pattyn1-Oct-09 6:22 
GeneralRe: socket problem: works in visual studio, not as release Pin
TimWallace1-Oct-09 6:56
TimWallace1-Oct-09 6:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.