如何在C#中进行无名称空间的简单XML模式验证

|| 我已经使用xsd.exe生成了一组类,并根据生成的代码创建了XML文档。我现在想针对原始xsd验证序列化的类实例。 我的XML是这样的:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<MyRoot xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">
   -- rest of XML document here
</MyRoot>
我的XSD是这样的:
<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">
   <xs:element name=\"MyRoot\" type=\"MyRootType\"/>
   -- MyRootType definition and rest of XSD
</xs:schema>
当我尝试使用XmlReader验证XML时,出现以下错误: \“未声明\'MyRoot \'元素。\” 有什么事吗     
已邀请:
        在MyRoot元素中,您需要添加XSD的位置。我还建议定义名称空间(除非您有充分的理由不这样做)。
<api:MyRoot  xmlns:xsi=\'http://www.w3.org/2001/XMLSchema-instance\'
   xmlns:api=\'http://www.myserver.com/schema\'
   xsi:schemaLocation=\'http://www.myserver.com/schema http://www.myserver.com/schema/websuiterecord.xsd\'>
</api:MyRoot>
这样,验证工具就会知道在哪里可以找到XSD来验证XML。     
        该方法是正确的,但实际上并没有读取XSD。我更正了此问题,它按预期工作。     

要回复问题请先登录注册