Schema obsoleto
19/11/2009
0
Meu projeto era .net 1.1 e estou no processo de migração para .net 3.5. O unico problema que estou encontrando é nas permissões e validações do arquivo XML. No projeto antigo funcionava direito, mas com a migração parou de funcionar.
Estava debugando o projeto e me deparei com a seguinte warning:
"'System.Xml.Schema.XmlSchemaCollection' is obsolete: Use System.Xml.Schema.XmlSchemaSet for schema compilation and validation." O codigo: private void Validate(String filename) { //ORIGINAL--------------------------------------------------------------------------------------------------------- string TheXsd = HttpContext.Current.Server.MapPath(@"SDP.xsd"); XmlSchemaCollection xsc = new XmlSchemaCollection(); xsc.Add(null, TheXsd); XmlTextReader reader = null; XmlValidatingReader vreader = null; reader = new XmlTextReader(filename); vreader = new XmlValidatingReader(reader); vreader.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); vreader.Schemas.Add(xsc); //vreader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack); try { while (vreader.Read()) { } } catch { //XML Document is not well-formed. throw; } vreader.Close();---------------------------------------------------------------------------------------------------------------------------------------------
Tava pesquisando como poderia substituir o metodo XmlSchemaCollection pelo XmlSchemaSet.
Alguem teria alguma ideia?
Será que isso ta influenciando nas permissões?
[]'s
Estava debugando o projeto e me deparei com a seguinte warning:
"'System.Xml.Schema.XmlSchemaCollection' is obsolete: Use System.Xml.Schema.XmlSchemaSet for schema compilation and validation." O codigo: private void Validate(String filename) { //ORIGINAL--------------------------------------------------------------------------------------------------------- string TheXsd = HttpContext.Current.Server.MapPath(@"SDP.xsd"); XmlSchemaCollection xsc = new XmlSchemaCollection(); xsc.Add(null, TheXsd); XmlTextReader reader = null; XmlValidatingReader vreader = null; reader = new XmlTextReader(filename); vreader = new XmlValidatingReader(reader); vreader.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); vreader.Schemas.Add(xsc); //vreader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack); try { while (vreader.Read()) { } } catch { //XML Document is not well-formed. throw; } vreader.Close();---------------------------------------------------------------------------------------------------------------------------------------------
Tava pesquisando como poderia substituir o metodo XmlSchemaCollection pelo XmlSchemaSet.
Alguem teria alguma ideia?
Será que isso ta influenciando nas permissões?
[]'s
Otton Moura
Curtir tópico
+ 0
Responder
Posts
23/11/2009
Netasper
Amigo,
Neste caso é so substituir o que esta obsoleto pelo atual sugerido, segue abaixo um exemplo:
try
{
System.IO.StringReader str_xml_string_reader = new System.IO.StringReader(txt_xmlsaft.Text);
System.IO.Stream str_xml_stream = flp_ficheiro.FileContent;
string url_xsl = string.Format("http://www.saftpt.com/docs/saf-t-pt.xsd", HttpContext.Current.Request.Url.AbsoluteUri.Replace(pagina, ""));
string url_xsl_new = string.Format("http://www.saftpt.com/docs/saftpt_1_01.xsd", HttpContext.Current.Request.Url.AbsoluteUri.Replace(pagina, ""));
// XmlSchemaSet
System.Xml.Schema.XmlSchemaSet sc = new System.Xml.Schema.XmlSchemaSet();
if (rbl_tipo_validacao.SelectedItem.Value == "Schema")
sc.Add("urn:OECD:StandardAuditFile-Tax:PT_1.00_01", url_xsl);
else
sc.Add("urn:OECD:StandardAuditFile-Tax:PT_1.01_01", url_xsl_new);
//validation settings
System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
settings.ProhibitDtd = false; // internet explorer error
settings.ValidationType = (rbl_tipo_validacao.SelectedItem.Value == "Schema" || rbl_tipo_validacao.SelectedItem.Value == "NewSchema") ? System.Xml.ValidationType.Schema : System.Xml.ValidationType.None;
settings.Schemas = sc;
settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(ValidationCallBack);
//XmlReader
System.Xml.XmlReader reader;
reader = (ficheiro_texto) ? System.Xml.XmlReader.Create(str_xml_stream, settings) : System.Xml.XmlReader.Create(str_xml_string_reader, settings);
//Restrição do elemento de topo
if (!reader.IsStartElement("AuditFile"))
{
Exception ex1 = new Exception(@"Elemento de topo inválido.O ficheiro xml deverá inicial com o elemento:""AuditFile""");
throw ex1;
}
//Percorre o ficheiro
while (reader.Read());
reader.Close();
if (valido)
resultado = string.Format("Documento válido em por ", settings.ValidationType.ToString().Replace("None", "XML"), (ficheiro_texto ? "ficheiro" : "texto"));
else
resultado = string.Format("Documento não válido em por ", settings.ValidationType.ToString().Replace("None", "XML"), (ficheiro_texto ? "ficheiro" : "texto"));
lbl_resultado.Text = resultado;
lbl_erros.Text = erro;
}
catch(Exception e1)
{
lbl_resultado.Text = "";
lbl_erros.Text = string.Format("<span style=\"color:#FF0000\">Erro no validador</span>:</br></br><span style=\"color:#00CE18\">Teste o validador utilizando outros browsers Internet Explorer | Firefox | Opera </span>", e1.Message);
}
Caso queira aprofundar mais, de uma olhada neste artigo: https://www.devmedia.com.br/articles/viewcomp_forprint.asp?comp=10744 []'s
{
System.IO.StringReader str_xml_string_reader = new System.IO.StringReader(txt_xmlsaft.Text);
System.IO.Stream str_xml_stream = flp_ficheiro.FileContent;
string url_xsl = string.Format("http://www.saftpt.com/docs/saf-t-pt.xsd", HttpContext.Current.Request.Url.AbsoluteUri.Replace(pagina, ""));
string url_xsl_new = string.Format("http://www.saftpt.com/docs/saftpt_1_01.xsd", HttpContext.Current.Request.Url.AbsoluteUri.Replace(pagina, ""));
// XmlSchemaSet
System.Xml.Schema.XmlSchemaSet sc = new System.Xml.Schema.XmlSchemaSet();
if (rbl_tipo_validacao.SelectedItem.Value == "Schema")
sc.Add("urn:OECD:StandardAuditFile-Tax:PT_1.00_01", url_xsl);
else
sc.Add("urn:OECD:StandardAuditFile-Tax:PT_1.01_01", url_xsl_new);
//validation settings
System.Xml.XmlReaderSettings settings = new System.Xml.XmlReaderSettings();
settings.ProhibitDtd = false; // internet explorer error
settings.ValidationType = (rbl_tipo_validacao.SelectedItem.Value == "Schema" || rbl_tipo_validacao.SelectedItem.Value == "NewSchema") ? System.Xml.ValidationType.Schema : System.Xml.ValidationType.None;
settings.Schemas = sc;
settings.ValidationEventHandler += new System.Xml.Schema.ValidationEventHandler(ValidationCallBack);
//XmlReader
System.Xml.XmlReader reader;
reader = (ficheiro_texto) ? System.Xml.XmlReader.Create(str_xml_stream, settings) : System.Xml.XmlReader.Create(str_xml_string_reader, settings);
//Restrição do elemento de topo
if (!reader.IsStartElement("AuditFile"))
{
Exception ex1 = new Exception(@"Elemento de topo inválido.O ficheiro xml deverá inicial com o elemento:""AuditFile""");
throw ex1;
}
//Percorre o ficheiro
while (reader.Read());
reader.Close();
if (valido)
resultado = string.Format("Documento válido em por ", settings.ValidationType.ToString().Replace("None", "XML"), (ficheiro_texto ? "ficheiro" : "texto"));
else
resultado = string.Format("Documento não válido em por ", settings.ValidationType.ToString().Replace("None", "XML"), (ficheiro_texto ? "ficheiro" : "texto"));
lbl_resultado.Text = resultado;
lbl_erros.Text = erro;
}
catch(Exception e1)
{
lbl_resultado.Text = "";
lbl_erros.Text = string.Format("<span style=\"color:#FF0000\">Erro no validador</span>:</br></br><span style=\"color:#00CE18\">Teste o validador utilizando outros browsers Internet Explorer | Firefox | Opera </span>", e1.Message);
}
Caso queira aprofundar mais, de uma olhada neste artigo: https://www.devmedia.com.br/articles/viewcomp_forprint.asp?comp=10744 []'s
Responder
Clique aqui para fazer login e interagir na Comunidade :)