I have to read an XML from file, which is:
<?xml version="1.0" encoding="UTF-16"?>
<KyactusProfileClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<username>sadasdsad</username>
<userid>0067AA87BF9AD466792E1A20F6AAB7F</userid>
<useDefaultFolder>false</useDefaultFolder>
<autoAcceptDownloads>false</autoAcceptDownloads>
<visible>false</visible>
</KyactusProfileClass>
I read it using following code:
using (var xmlReader = XmlReader.Create(pathXML))
{
Console.WriteLine(File.ReadAllText(pathXML));
schemas.Add(null, xmlReader);
}
But I get following error:
System.Xml.Schema.XmlSchemaException: 'The root element of W3C XML Schema must be < schema > and its namesapce must be 'http://www.w3.org/2001/XMLSchema'.'
I generated that XML using a XML serializer and XSD Schema validator:
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="kyactusprofile" type="kyactusprofile" />
<xs:complexType name="kyactusprofile" >
<xs:sequence>
<xs:element type="xs:string" name="username" maxOccurs="1" minOccurs="1"/>
<xs:element type="xs:string" name="userid" maxOccurs="1" minOccurs="1"/>
<xs:element type="xs:boolean" name="useDefaultFolder" maxOccurs="1" minOccurs="1"/>
<xs:element type="xs:string" name="defaultFolder" maxOccurs="1" minOccurs="1"/>
<xs:element type="xs:boolean" name="autoAcceptDownloads" maxOccurs="1" minOccurs="1"/>
<xs:element type="xs:boolean" name="visible" />
</xs:sequence>
</xs:complexType>
</xs:schema>
Hers is the stack trace:
System.Xml.Schema.XmlSchemaException
HResult=0x80131941
Message=L'elemento radice di W3C XML Schema deve essere <schema> e il suo spazio dei nomi deve essere 'http://www.w3.org/2001/XMLSchema'.
Source=System.Xml
StackTrace:
at System.Xml.Schema.XmlSchemaSet.InternalValidationCallback(Object sender, ValidationEventArgs e)
at System.Xml.Schema.XmlSchemaSet.SendValidationEvent(XmlSchemaException e, XmlSeverityType severity)
at System.Xml.Schema.XmlSchemaSet.ParseSchema(String targetNamespace, XmlReader reader)
at System.Xml.Schema.XmlSchemaSet.Add(String targetNamespace, XmlReader schemaDocument)
at Kyactus.XmlManager.GestoreXml.ValidateXmlWithXsd(String pathXML) in C:\Users\Cristiano\Documents\Visual Studio 2017\Projects\progetto-pds\visual-studio\Kyactus\Kyactus\XmlManager\GestoreXml.cs:line 249
at Kyactus.XmlManager.GestoreXml.UnmarshallProfile() in C:\Users\Cristiano\Documents\Visual Studio 2017\Projects\progetto-pds\visual-studio\Kyactus\Kyactus\XmlManager\GestoreXml.cs:line 178
at Kyactus.XmlManager.GestoreXml..ctor() in C:\Users\Cristiano\Documents\Visual Studio 2017\Projects\progetto-pds\visual-studio\Kyactus\Kyactus\XmlManager\GestoreXml.cs:line 64
at Kyactus.App..ctor() in C:\Users\Cristiano\Documents\Visual Studio 2017\Projects\progetto-pds\visual-studio\Kyactus\Kyactus\App.xaml.cs:line 21
at Kyactus.App.Main()
You are most likely reading the XML
<?xml version="1.0" encoding="UTF-16"?>
<KyactusProfileClass xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<username>sadasdsad</username>
<userid>0067AA87BF9AD466792E1A20F6AAB7F</userid>
<useDefaultFolder>false</useDefaultFolder>
<autoAcceptDownloads>false</autoAcceptDownloads>
<visible>false</visible>
</KyactusProfileClass>
with the XmlReader
and try to add it as XmlSchema
. That's the wrong approach, it's not a schema! You have to read your XSD
, then no Exception
will appear.
User contributions licensed under CC BY-SA 3.0