Connecting Tech Pros Worldwide Help | Site Map

XML validation error. Help required

stuart dent via .NET 247
Guest
 
Posts: n/a
#1: Nov 12 '05
XML validation error. Help required
If anyone can help me, thankyou, thankyou...

When I run this code code I get this error:
The data at the root level is invalid. Line 1, position 39.

I can't work out what the error is. Can you???

original code:
Dim oRead As XmlTextReader
Dim oValid As XmlValidatingReader

Try
oRead = New XmlTextReader("C:\custdoc.xml")
oValid = New XmlValidatingReader(oRead)

AddHandler oValid.ValidationEventHandler, New Schema.ValidationEventHandler(AddressOf ValidationError)
oValid.Schemas.Add("newschema", "C:\custschema.xsd")
oValid.ValidationType = ValidationType.Auto

Do While oValid.Read
'let validation do its thing
Loop...


xml doc file:
<?xml version="1.0" encoding="UTF-8"?>
<myroot xmlns:xx="generic">
<xx:Customer>
<xx:ID>"000001"</xx:ID>
<xx:Name>"CustOne"</xx:Name>
</xx:Customer>
<xx:Customer>
<xx:ID>000002</xx:ID>
<xx:Name>CustTwo</xx:Name>
</xx:Customer>
</myroot>

xml schema file:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="generic" elementFormDefault="qualified" targetNamespace="generic">
<xs:element name="myroot">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="Customer">
<xs:complexType>
<xs:sequence>
<xs:element name="ID" type="xs:string" minOccurs="0" />
<xs:element name="Name" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>

Thanks, Stu
--------------------------------
From: stuart dent

-----------------------
Posted by a user from .NET 247 (http://www.dotnet247.com/)

<Id>pVvJYVwbZUevEQlnr5NghQ==</Id>
Derek Harmon
Guest
 
Posts: n/a
#2: Nov 12 '05

re: XML validation error. Help required


"stuart dent via .NET 247" <anonymous@dotnet247.com> wrote in message news:OUCL5P1mEHA.2076@TK2MSFTNGP15.phx.gbl...[color=blue]
> When I run this code code I get this error:
> The data at the root level is invalid. Line 1, position 39.[/color]
: :[color=blue]
> <?xml version="1.0" encoding="UTF-8"?>[/color]

Anytime you receive an XmlException where the position is at the very end of the XML declaration,
the XML declaration has no syntax errors, and that XML declaration has an "encoding" pseudo-
attribute, then it means the file is _not_ whatever that encoding pseudo-attribute says.

Therefore, the file is not UTF-8 encoded. Try loading it into Notepad and then doing File | Save As..
and choosing UTF-8 from the Encoding drop-down box. Alternately, change the encoding pseudo-
attribute to indicate what the encoding of the file truly is.

The file that's the culprit depends on whether the XmlException was thrown from the XmlTextReader
(it's then the instance document that has the invalid encoding) or the Add( ) to the XmlSchemaCollection
(it's then the schema document that has the invalid encoding).


Derek Harmon


Closed Thread