我必须从文件中读取 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>
我使用以下代码阅读它:
using (var xmlReader = XmlReader.Create(pathXML))
{
Console.WriteLine(File.ReadAllText(pathXML));
schemas.Add(null, xmlReader);
}
但我收到以下错误:
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'.'
我使用 XML 序列化器和 XSD 架构验证器生成了该 XML:
<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>
她的是堆栈跟踪:
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()
最佳答案
您很可能正在阅读 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>
使用 XmlReader
并尝试将其添加为 XmlSchema
。这是错误的方法,它不是一个模式!您必须读取您的XSD
,然后就不会出现Exception
。
关于c# - XmlSchemaException C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49688677/