473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to validate, on run time, xml against xsd without save the file on local folder?

2
I need to validate XMLs files against XSDs. The XML will be downloaded from URL and I will keep it as StremReader or XmlDocument. The XSD will return from DataBase as nvarchar(max). I am prohibited to save the files locally. Has anybody an example how to deal with this situation? I am trying this way but I am getting the XmlException "Root element is missing".


public void voltaXsd_em_StreamReader()
{
//strArquivoInteiro will contain the XSD comming from database as nvarchar(max) but I make simpler here.
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\file.xsd");
string strArquivoInteiro = xmlDoc.OuterXml;

byte[] byteArray = Encoding.ASCII.GetBytes(strArquivoInteiro);
MemoryStream streamXSD = new MemoryStream(byteArray);

StreamReader readerXsd = new StreamReader(streamXSD);

XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += this.ValidationEventHandler;

settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add("schema.xsd", XmlReader.Create(readerXsd));
settings.CheckCharacters = true;

XmlReader XmlValidatingReader = XmlReader.Create(@"C:\file.xml", settings);

XmlTextReader Reader = new XmlTextReader(@"file.xml");

XmlSchema Schema = new XmlSchema();

//Exactlly here I am getting the exception "Root element is missing" and I do not know why.

Schema = XmlSchema.Read(readerXsd, ValidationEventHandler);

XmlValidatingReader ValidatingReader = new XmlValidatingReader(Reader);

//// 6- Setar o tipo de validação para o objeto XmlValidationReader
ValidatingReader.ValidationType = ValidationType.Schema;

//// 7- Adicionar Schema a coleção de Schemas XmlValidationReader
ValidatingReader.Schemas.Add(Schema);

try
{
//---------------

XmlValidatingReader.Read();
XmlValidatingReader.Close();

//// 8- Adicionar o endereço do ValidationEventHandler ao ValidationEventHandler do XmlValidationReader
ValidatingReader.ValidationEventHandler += ValidationEventHandler;


//9- Validar cada nó
while ((ValidatingReader.Read()))
{

}


ValidatingReader.Close();
}
catch (Exception ex)
{
ValidatingReader.Close();
XmlValidatingReader.Close();

}
}
private void ValidationEventHandler(object sender, ValidationEventArgs args)
{
bool blnXmlValido;
if (args.Severity == XmlSeverityType.Warning)
{

blnXmlValido = false;
}
else if (args.Severity == XmlSeverityType.Error)
{

blnXmlValido = false;
}
else if (!(string.IsNullOrEmpty(args.Exception.ToString()) ))
{

blnXmlValido = false;
}

if ((args.Exception != null))
{

}
}
Jun 18 '10 #1
0 1211

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Tommy | last post by:
Is it possible to write asp file which create some text file and then save in local drive automatically without human interaction? I haven't any idea. Thx.
4
by: Yuri Vorontsov | last post by:
Hallo! We have troubles (post XP SP2) to open local folders from the web application: - the web application allows users to select a local file (input type=file) - the system DOES NOT upload...
9
by: Ivan Demkovitch | last post by:
Hi! I would like to know if I can save File on Server using server-side code? For example, I like to create thumbnail images and populate specific directory. Do I need specific permissions...
5
by: _DG | last post by:
At least when you've got any docs in .CHM format in the folder tree. If you drop a file called CSharpDocs.chm into a folder: \toplevel\C#\docs, you can open it, but it won't display. The #...
2
by: John H | last post by:
Hi, How can i just use the XmlDocument object to validate an xml instanace against a schema referenced inside the xml instance? The Load method seems to not validate it against the schema. ...
4
by: Jonny | last post by:
Hello Group How do I open a Save File Dialog from an ASPX page behind a browse button? Any help would be fantastic!! I am using ASP.NET 1.1 using VB.NET as the coding language TIA
0
by: jason | last post by:
I googled a lot, couldn't find a good solution. Any help is greatly appreciated. What I want to do is: Given a web page in a running IE browser , i want to save an image in this web page to...
0
by: ric_deez | last post by:
Hi there, I have just started looking into using Python to perform some XML processing and I need to ensure that it is capable of validating against a schema file (XSD not DTD!). The research...
6
by: amjad | last post by:
Hi i have dataset that i want to save to local disk as xml file. i dont know how to save it to local disk instead of server thanks
4
by: krishna81m | last post by:
A very interesting problem for a simple requirement and not easily available solution: How would I create a button which will allow the user to specify where the file to be downloaded will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.