473,395 Members | 1,456 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,395 software developers and data experts.

How to validate, xml against xsd while the xsd is in a string variable?

2
Hello. My objective is to validade a xml file against the xsd whitch is in a string variable.


/*
book.xml
<?xml version="1.0" encoding="utf-8"?>
<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>

* book.xsd
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="NewDataSet" targetNamespace="urn:bookstore-schema" xmlns:mstns="urn:bookstore-schema" xmlns="urn:bookstore-schema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
<xs:element name="author">
<xs:complexType>
<xs:sequence>
<xs:element name="first-name" type="xs:string" minOccurs="0" />
<xs:element name="last-name" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="author" />
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
*/
//The Xsd_after_saved() is woorking perfectlly
//In Xsd_after_saved(), every place I need to use the XSD, I got from file locally
public void Xsd_after_saved()
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += this.ValidationEventHandler;

settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, XmlReader.Create(@"C:\book.xsd"));
settings.CheckCharacters = true;

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

XmlTextReader Reader = new XmlTextReader(@"C:\book.xml");

StreamReader SR = new StreamReader(@"C:\book.xsd");

XmlSchema Schema = new XmlSchema();

Schema = XmlSchema.Read(SR, ValidationEventHandler);

XmlValidatingReader ValidatingReader = new XmlValidatingReader(Reader);

ValidatingReader.ValidationType = ValidationType.Schema;

ValidatingReader.Schemas.Add(Schema);

try
{
XmlValidatingReader.Read();
XmlValidatingReader.Close();

ValidatingReader.ValidationEventHandler += ValidationEventHandler;

while ((ValidatingReader.Read()))
{
}

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

}
}

//The Xsd_whithout_saved() is not working
//In Xsd_whithout_saved(), every place I need the XSD, I got from variable StreamReader named readerXsd whitch come from a string
public void Xsd_whithout_saved()
{
//>>>Here is the biggest diference from the method Xsd_after_saved: I manipulate the XSD as string because it will come from database and
//it will not allowed to be saved locally
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\book.xsd");
//In the futute, strArquivoInteiro will be fullfill by xsd comming from database as nvarchar(max) and I will not be allowed to save as a file locally
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(null, XmlReader.Create(readerXsd));
settings.CheckCharacters = true;

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

XmlTextReader Reader = new XmlTextReader(@"C:\book.xml");

XmlSchema Schema = new XmlSchema();

//IN THIS LINE I RECEIVED THE XmlException "Root Element is Missing" and I can't understand the reason
Schema = XmlSchema.Read(readerXsd, ValidationEventHandler);

XmlValidatingReader ValidatingReader = new XmlValidatingReader(Reader);

ValidatingReader.ValidationType = ValidationType.Schema;

ValidatingReader.Schemas.Add(Schema);

try
{

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

ValidatingReader.ValidationEventHandler += ValidationEventHandler;

while ((ValidatingReader.Read()))
{

}


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

}
}
private void ValidationEventHandler(object sender, ValidationEventArgs args)
{
//place to deal with xml file no valided
}
Jun 21 '10 #1
0 1472

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

Similar topics

16
by: PK9 | last post by:
I have a string variable that holds the equivalent of a DateTime value. I pulled this datetime from the database and I want to strip off the time portion before displaying to the user. I am...
3
by: XenofeX | last post by:
How can i call to object from string variable ? For instance string x = "textBox1"; i want to call the object which name is stored in the x string variable. The most important thing is the...
3
by: CK | last post by:
I am doing a Doc.Loadxml(myxml) and I have an xsd schema that I want to validate the myxml against. How can I do this. code would help. Thanks in advance.
6
by: Peter Afonin | last post by:
Hello, Should be a pretty simple question: I need to validate a string. It must be numeric and contain exactly 12 characters. I know how to do it in code, but I'd like to use a validation...
6
by: Friso Wiskerke | last post by:
Hi All, I'm trying to find a way to validate a string variable in the code behind with a regular expression. I know there are validator controls but these all work with the ControlToValidate...
4
by: JohnR | last post by:
Hi all, I'm finally sick and tired of manually generating get/set properties for each private variable in a class so I'm trying to create a macro to do it. I'm stuck because I can't figure out...
2
by: Kevin | last post by:
Hi All, I want to validate a string, and see if it contains any Chinese character (simple or traditional). I'm trying to use RegExp and Encoding, but no result. Can someone point me a...
3
by: c676228 | last post by:
Hi everyone, I just realized that it's so important to validate each string, I mean 'each' before you insert data from asp page into database. I guess some customers just copy data from some...
6
by: John Kotuby | last post by:
Hi all, I am using a 3rd party program in a VS2005 web project. The tool takes as input a string containing HTML and converts it to RTF. I have been creating a page by dynamically loading...
2
by: Looch | last post by:
All, I'm trying to output but I can only get (brackets for clarity) when using the code below. How can I "break" into the query variable in the InsertName method to add the name parameter to...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.