Quick question about XML manipulation

B

B. Chernick

I have just been told I need to write a quickie throwaway program that
generates an XML file. I am a bit rusty. The program will be written using
VB in Visual Studio 2005.

Do I understand this correctly - If I include a reference to an XSD or DTD
file at the start of the XMLDocument object, there will be automatic
validation of the data as it is added to the structure? (Assuming the XSD or
DTD is in an accessible location?)
 
M

Morten Wennevik [C# MVP]

B. Chernick said:
I have just been told I need to write a quickie throwaway program that
generates an XML file. I am a bit rusty. The program will be written using
VB in Visual Studio 2005.

Do I understand this correctly - If I include a reference to an XSD or DTD
file at the start of the XMLDocument object, there will be automatic
validation of the data as it is added to the structure? (Assuming the XSD or
DTD is in an accessible location?)

Hi B,

You probably have figured it out already, but for the record, yes and no,
specifying a schema will allow you to validate against it, but the validation
does not occur as you add elements to the XmlDocument. Validation happens
only when you specifically call XmlDocument.Validate()

public void Method()
{
XmlDocument doc = new XmlDocument();

XmlSchema schema = new XmlSchema();
schema.SourceUri = @"C:\Input.xsd";
doc.Schemas.Add(schema);

// Valid root element according to the schema
// XmlElement element = doc.CreateElement("Input",
"http://temp.org/Input");

// Invalid root element. The 'Input' element is not declared.
XmlElement element = doc.CreateElement("Input");
doc.AppendChild(element);

ValidationEventHandler handler = new
ValidationEventHandler(ValidationMethod);

doc.Validate(handler);
}

public void ValidationMethod(object sender, ValidationEventArgs e)
{
MessageBox.Show(e.Message, "The xml validation failed");
}
 
B

B. Chernick

Thanks!

(If memory serves me correctly, I've also seen examples of this technique
using a DTD file.)
 
B

B. Chernick

I think I spoke too soon. While I think I can more or less follow this code,
I have another issue: What I would like to do is have both the XML and the
DTD as Resources files. So far I'm not sure how or even if a DTD can be
fitted into this kind of code.
 
M

Morten Wennevik [C# MVP]

Hi,

I'm afraid I haven't worked with DTD files so I'm unsure what support there
is for it in XmlDocument. I know you can validate against one using a
XmlValidatingReader

http://support.microsoft.com/kb/307379

but I couldn't find any usage of it in XmlDocument or XmlWriter other than
adding the DocType to the Xml. You could always do a post validation of a
finished XmlDocument by reading it with an XmlValidatingReader though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top