我必须从文件中读取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:'W3C XML Schema的根元素必须是&lt;架构&gt;它的名字必须是'http://www.w3.org/2001/XMLSchema'。'
我使用XML序列化程序和XSD Schema验证器生成了该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()
答案 0 :(得分:4)
您最有可能阅读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
。