newbie: XmlTextWriter + xsd

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Hello!

IDE: VS 2003 .Net
OS: XP Pro

I'm experimenting with C# for the purpose to learn it... I've made a test
program that create a simple xml-file using XmlTextWriter. Now I want to
extend that logic so the xml-file references a xsd-file...

I know that if XmlTextWriter begin to write data to a xml-file that already
contains data, the contents is truncated, so everything must be created
again through the XmlTextWriter.

My problem is this:
How do I link a xsd-file to a xml-file using XmlTextWriter???

Here is some of the approaches I tryed:
XmlTextWriter writer = new XmlTextWriter(dlgOpen.FileName, null);
writer.WriteStartDocument();
writer.WriteDocType("Countries", null, "test.xsd", null);
writer.WriteStartElement("Countries");
// some computing
writer.WriteEndDocument();
When the resulting xml-file is displayed in IE, I get this error message
"The XML page cannot be displayed"
So this approach is wrong I guess!!


I've searched the internet (for 1 day) for clues about this, but so far
without any luck so far. Lot of info there about xml, but I found non that
explains my problem...

Any clues will be appreciated!

Regards!

Jeff
 
Following is some code which will help to create an XML file, my.xml, using an xsd file, test.xsd

private void btnWriteXMLFile_Click(object sender, System.EventArgs e)
{
XmlTextWriter writer = new XmlTextWriter("my.xml",null);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement(null,"Countries",null);
writer.WriteAttributeString(null,"xmlns",null,"http://www.someurl.com");
writer.WriteAttributeString("xsi","schemaLocation","http://www.someurlcom","test.xsd");
writer.WriteElementString("Title",null,"India");
writer.WriteEndElement();
writer.Close();
}

private void btnReadXMLFile_Click(object sender, System.EventArgs e)
{
XmlTextReader reader = new XmlTextReader("my.xml");
XmlValidatingReader vr = new XmlValidatingReader(reader);
vr.ValidationType = System.Xml.ValidationType.Schema;
try
{
/*you may replace this code with statements which will display the contents of the file or do some other processing with the file contents */

vr.Read();
vr.Read();
vr.ReadStartElement();
vr.ReadElementString();
vr.ReadEndElement();
}
catch (XmlException ex)
{
MessageBox.Show(ex.Message,ex.Source);
}
finally
{
vr.Close();
reader.Close();
}
}

Following is the test.xsd file contents:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema targetNamespace="http://www.someurl.com"
elementFormDefault="qualified"
xmlns="http://www.someurl.com"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Countries">
<xs:complexType>
<xs:all>
<xs:element name="Title"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>

You can experiment with btnWriteXMLFile_Click method: Add some elements which are not specified in test.xsd, or remove the Title element. The btnReadXMLFile_click method will generate exceptions, which is an indication that the XML file is read as per the xsd specification.

The WriteDocType() method is used when you want XML file to refer to a DTD and not to a schema.

Hope this helps
 
Back
Top