Groups | Blog | Home
all groups > dotnet xml > july 2004 >

dotnet xml : Newbie: trouble validating xml file



Jeff
7/14/2004 11:08:03 AM
IDE: Visual Studio 2003 .NET (7.1.3088)
OS: XP Pro
..NET Framework 1.1 (1.1.4322)

I'm experimenting with xml in .net, and having trouble to get a xml file
validated without any errors/warnings in VS . So I searched the web and
found this example at
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q307379&ID=kb;en-us;Q307379&SD=MSDN
section "Use Namespaces in the XSD Schema"

ProductWithXSD.xml:
<?xml version="1.0"?>
<Product ProductID="123"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:MyNamespace"
xsi:schemaLocation="urn:MyNamespace Product.xsd">
<ProductName>Rugby jersey</ProductName>
</Product>

Product.xsd:
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:MyNamespace" elementFormDefault="qualified">
<xsd:element name="Product">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="ProductName" type="xsd:string"/>
</xsd:sequence>
<xsd:attribute name="ProductID" use="required" type="xsd:int"/>
</xsd:complexType>
</xsd:element>
</xsd:schema>

When I in VS chooses XML | Validate XML Data I get these errors (XmlSpy
reports that both files are valid):

C:\MyProjects\Visual Studio Projects\ReadXml\support\ProductWithXSD.xml(1):
Visual Studio could not locate a schema for this document. Validation can
only ensure this is a well formed XML document and cannot validate the data
against a schema.

C:\MyProjects\Visual Studio Projects\ReadXml\support\ProductWithXSD.xml(2):
The 'urn:MyNamespace:Product' element is not declared. An error occurred at
, (2, 2).

C:\MyProjects\Visual Studio Projects\ReadXml\support\ProductWithXSD.xml(6):
The 'urn:MyNamespace:ProductName' element is not declared. An error occurred
at , (6, 5).

In addition. When I'm in the schema editor in VS, I get an error when
switching from xml view to schema view: "Document Element tag xsd:schema
must indentify a qualifier for the targetNamespace "urn:MyNamespace""

Any clues to why I get these errors will be appreciated!

Jeff

Chris Lovett
7/15/2004 8:29:43 AM
The XmlTextReader cannot resolve the external Product.xsd if you give it a
Stream, since the Stream class does not provide a way to get the base URI.
Try passing the full path of the file ProductWithXSD.xml the XmlTextReader.

If you cannot do that for some reason, then try creating a custom
XmlResolver and setting this on the XmlTextReader, then override ResolveUri
and GetEntity to find the schema that way.

[quoted text, click to view]

AddThis Social Bookmark Button