Parsear XML con DTD desde codigo

21/10/2004 - 17:00 por Oriol | Informe spam
Hola a todos,

Tengo un problema que quiero solucionar. Estoy haciendo una aplicacion en C#
para consola, donde manipulo unos datos hasta conseguir un fichero XML.
Hasta aqui he llegado sin muchos problemas. El problema que se me plantea
ahora es el de parsear el fichero XML para ver si es valido o no segun un
fichero DTD. Esto lo puedo hacer en ventana con un programa que ya tengo que
se llama xmlint.exe que es un parseador por linia de comandos. Asi que creo
tengo dos opciones, una es buscar si C# permite parsear un fichero
directamente (que no se como), o ejecutar este proceso externo. Tambien debo
decir que necesito que se me devuelva un codigo para saber si esta bien o
no.

Alguien sabe como hacerlo y me podria poner un poco de codigo para verlo?

Mil gracias!!!

Saludos,
Oriol.
 

Leer las respuestas

#1 Cesarion
21/10/2004 - 18:25 | Informe spam
Bueno espero que lo siguiente te sirva, lo encontre en la documentación de
VS. Cualquier cosa me cuentas. bye

Document type definition (DTD) validation is implemented using the validity
constraints defined in the World Wide Web Consortium (W3C) Extensible Markup
Language (XML) 1.0 Recommendation. DTDs use a formal grammar to describe the
structure and syntax of compliant XML documents; they specify content and
values allowed for the XML document.

To perform validation against a document type definition (DTD), the
XmlValidatingReader uses the DTD defined in the DOCTYPE declaration of an XML
document. The DOCTYPE declaration can either point to an inline DTD or can be
a reference to an external DTD file.

The following code example creates an XmlValidatingReader that takes an
XmlTextReader. The input file, HeadCount.xml, is validated against an
external DTD file, HeadCount.dtd. Any severity types and error messages are
displayed.

[Visual Basic]
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Schema

public class ValidationSample

public shared sub Main()
Dim tr As XmlTextReader = new XmlTextReader("HeadCount.xml")
Dim vr As XmlValidatingReader = new XmlValidatingReader(tr)

vr.ValidationType = ValidationType.DTD
AddHandler vr.ValidationEventHandler, AddressOf ValidationCallback
while(vr.Read())
end while
Console.WriteLine("Validation finished")

end sub

public shared sub ValidationCallBack (sender As object, args As
ValidationEventArgs)

Console.WriteLine("***Validation error")
Console.WriteLine("Severity:{0}", args.Severity)
Console.WriteLine("Message:{0}", args.Message)
end sub
end class

[C#]
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

namespace ValidationSample
{
class Sample
{
public static void Main()
{
XmlTextReader tr = new XmlTextReader("HeadCount.xml");
XmlValidatingReader vr = new XmlValidatingReader(tr);

vr.ValidationType = ValidationType.DTD;
vr.ValidationEventHandler += new ValidationEventHandler
(ValidationHandler);

while(vr.Read());
Console.WriteLine("Validation finished");
}

public static void ValidationHandler(object sender,
ValidationEventArgs args)
{
Console.WriteLine("***Validation error");
Console.WriteLine("\tSeverity:{0}", args.Severity);
Console.WriteLine("\tMessage :{0}", args.Message);
}
}
}

The following outlines the contents of the input file, HeadCount.xml, to be
validated.

<!DOCTYPE HeadCount SYSTEM "HeadCount.dtd">
<HeadCount>
<Name First="Waldo" Last="Pepper">
<Name First="Salt" Last="Pepper" Relation="spouse"/>
<Name First="Red" Last="Pepper" Relation="child"/>
</Name>
<Name First="&MyFirst;" Last="&MyLast;">
<Name First="Sharon" Last="&MyLast;" Relation="spouse"/>
<Name First="Morgan" Last="&MyLast;" Relation="child"/>
<Name First="Shelby" Last="&MyLast;" Relation="child"/>
</Name>
</HeadCount>

The following outlines the contents of the external DTD file, HeadCount.dtd,
to be validated against.

<!ELEMENT HeadCount (Name)*>
<!ELEMENT Name (Name)*>
<!ATTLIST Name First CDATA #REQUIRED>
<!ATTLIST Name Last CDATA #REQUIRED>
<!ATTLIST Name Relation (self | spouse | child) "self">
<!ENTITY MyFirst "Jeff">
<!ENTITY MyLast "Smith">


"Oriol" escribió:

Hola a todos,

Tengo un problema que quiero solucionar. Estoy haciendo una aplicacion en C#
para consola, donde manipulo unos datos hasta conseguir un fichero XML.
Hasta aqui he llegado sin muchos problemas. El problema que se me plantea
ahora es el de parsear el fichero XML para ver si es valido o no segun un
fichero DTD. Esto lo puedo hacer en ventana con un programa que ya tengo que
se llama xmlint.exe que es un parseador por linia de comandos. Asi que creo
tengo dos opciones, una es buscar si C# permite parsear un fichero
directamente (que no se como), o ejecutar este proceso externo. Tambien debo
decir que necesito que se me devuelva un codigo para saber si esta bien o
no.

Alguien sabe como hacerlo y me podria poner un poco de codigo para verlo?

Mil gracias!!!

Saludos,
Oriol.



Preguntas similares