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:book store-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:bookst ore-schema" xmlns:mstns="ur n:bookstore-schema" xmlns="urn:book store-schema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="u rn:schemas-microsoft-com:xml-msdata" attributeFormDe fault="qualifie d" elementFormDefa ult="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="NewDataSe t" msdata:IsDataSe t="true" msdata:UseCurre ntLocale="true" >
<xs:complexType >
<xs:choice minOccurs="0" maxOccurs="unbo unded">
<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 ()
{
XmlReaderSettin gs settings = new XmlReaderSettin gs();
settings.Valida tionEventHandle r += this.Validation EventHandler;
settings.Valida tionType = ValidationType. Schema;
settings.Schema s.Add(null, XmlReader.Creat e(@"C:\book.xsd "));
settings.CheckC haracters = true;
XmlReader XmlValidatingRe ader = XmlReader.Creat e(@"C:\book.xml ", settings);
XmlTextReader Reader = new XmlTextReader(@ "C:\book.xm l");
StreamReader SR = new StreamReader(@" C:\book.xsd");
XmlSchema Schema = new XmlSchema();
Schema = XmlSchema.Read( SR, ValidationEvent Handler);
XmlValidatingRe ader ValidatingReade r = new XmlValidatingRe ader(Reader);
ValidatingReade r.ValidationTyp e = ValidationType. Schema;
ValidatingReade r.Schemas.Add(S chema);
try
{
XmlValidatingRe ader.Read();
XmlValidatingRe ader.Close();
ValidatingReade r.ValidationEve ntHandler += ValidationEvent Handler;
while ((ValidatingRea der.Read()))
{
}
ValidatingReade r.Close();
}
catch (Exception ex)
{
ValidatingReade r.Close();
XmlValidatingRe ader.Close();
}
}
//The Xsd_whithout_sa ved() is not working
//In Xsd_whithout_sa ved(), every place I need the XSD, I got from variable StreamReader named readerXsd whitch come from a string
public void Xsd_whithout_sa ved()
{
//>>>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, strArquivoIntei ro will be fullfill by xsd comming from database as nvarchar(max) and I will not be allowed to save as a file locally
string strArquivoIntei ro = xmlDoc.OuterXml ;
byte[] byteArray = Encoding.ASCII. GetBytes(strArq uivoInteiro);
MemoryStream streamXSD = new MemoryStream(by teArray);
//<<<
StreamReader readerXsd = new StreamReader(st reamXSD);
XmlReaderSettin gs settings = new XmlReaderSettin gs();
settings.Valida tionEventHandle r += this.Validation EventHandler;
settings.Valida tionType = ValidationType. Schema;
settings.Schema s.Add(null, XmlReader.Creat e(readerXsd));
settings.CheckC haracters = true;
XmlReader XmlValidatingRe ader = XmlReader.Creat e(@"C:\book.xml ", settings);
XmlTextReader Reader = new XmlTextReader(@ "C:\book.xm l");
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, ValidationEvent Handler);
XmlValidatingRe ader ValidatingReade r = new XmlValidatingRe ader(Reader);
ValidatingReade r.ValidationTyp e = ValidationType. Schema;
ValidatingReade r.Schemas.Add(S chema);
try
{
XmlValidatingRe ader.Read();
XmlValidatingRe ader.Close();
ValidatingReade r.ValidationEve ntHandler += ValidationEvent Handler;
while ((ValidatingRea der.Read()))
{
}
ValidatingReade r.Close();
}
catch (Exception ex)
{
ValidatingReade r.Close();
XmlValidatingRe ader.Close();
}
}
private void ValidationEvent Handler(object sender, ValidationEvent Args args)
{
//place to deal with xml file no valided
}
/*
book.xml
<?xml version="1.0" encoding="utf-8"?>
<author xmlns='urn:book store-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:bookst ore-schema" xmlns:mstns="ur n:bookstore-schema" xmlns="urn:book store-schema" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="u rn:schemas-microsoft-com:xml-msdata" attributeFormDe fault="qualifie d" elementFormDefa ult="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="NewDataSe t" msdata:IsDataSe t="true" msdata:UseCurre ntLocale="true" >
<xs:complexType >
<xs:choice minOccurs="0" maxOccurs="unbo unded">
<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 ()
{
XmlReaderSettin gs settings = new XmlReaderSettin gs();
settings.Valida tionEventHandle r += this.Validation EventHandler;
settings.Valida tionType = ValidationType. Schema;
settings.Schema s.Add(null, XmlReader.Creat e(@"C:\book.xsd "));
settings.CheckC haracters = true;
XmlReader XmlValidatingRe ader = XmlReader.Creat e(@"C:\book.xml ", settings);
XmlTextReader Reader = new XmlTextReader(@ "C:\book.xm l");
StreamReader SR = new StreamReader(@" C:\book.xsd");
XmlSchema Schema = new XmlSchema();
Schema = XmlSchema.Read( SR, ValidationEvent Handler);
XmlValidatingRe ader ValidatingReade r = new XmlValidatingRe ader(Reader);
ValidatingReade r.ValidationTyp e = ValidationType. Schema;
ValidatingReade r.Schemas.Add(S chema);
try
{
XmlValidatingRe ader.Read();
XmlValidatingRe ader.Close();
ValidatingReade r.ValidationEve ntHandler += ValidationEvent Handler;
while ((ValidatingRea der.Read()))
{
}
ValidatingReade r.Close();
}
catch (Exception ex)
{
ValidatingReade r.Close();
XmlValidatingRe ader.Close();
}
}
//The Xsd_whithout_sa ved() is not working
//In Xsd_whithout_sa ved(), every place I need the XSD, I got from variable StreamReader named readerXsd whitch come from a string
public void Xsd_whithout_sa ved()
{
//>>>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, strArquivoIntei ro will be fullfill by xsd comming from database as nvarchar(max) and I will not be allowed to save as a file locally
string strArquivoIntei ro = xmlDoc.OuterXml ;
byte[] byteArray = Encoding.ASCII. GetBytes(strArq uivoInteiro);
MemoryStream streamXSD = new MemoryStream(by teArray);
//<<<
StreamReader readerXsd = new StreamReader(st reamXSD);
XmlReaderSettin gs settings = new XmlReaderSettin gs();
settings.Valida tionEventHandle r += this.Validation EventHandler;
settings.Valida tionType = ValidationType. Schema;
settings.Schema s.Add(null, XmlReader.Creat e(readerXsd));
settings.CheckC haracters = true;
XmlReader XmlValidatingRe ader = XmlReader.Creat e(@"C:\book.xml ", settings);
XmlTextReader Reader = new XmlTextReader(@ "C:\book.xm l");
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, ValidationEvent Handler);
XmlValidatingRe ader ValidatingReade r = new XmlValidatingRe ader(Reader);
ValidatingReade r.ValidationTyp e = ValidationType. Schema;
ValidatingReade r.Schemas.Add(S chema);
try
{
XmlValidatingRe ader.Read();
XmlValidatingRe ader.Close();
ValidatingReade r.ValidationEve ntHandler += ValidationEvent Handler;
while ((ValidatingRea der.Read()))
{
}
ValidatingReade r.Close();
}
catch (Exception ex)
{
ValidatingReade r.Close();
XmlValidatingRe ader.Close();
}
}
private void ValidationEvent Handler(object sender, ValidationEvent Args args)
{
//place to deal with xml file no valided
}