C#:schema进行XML校验

gwang119 2008-07-15 01:25:37
XML:
<?xml version='1.0' encoding='UTF-16'?>
<Judge xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="Judge.xsd">
<Item id="01" name="aaa" effect="1">
<Check id="01" name="check1" type="and">
<Property id="FormatType" name="bbb" value="1"/>
<Property id="FormatType" name="ccc" value="2"/>
</Check>
</Item>
</Judge>


XSD:
<?xml version='1.0' encoding='utf-16'?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="Judge">
<xsd:complexType>
<xsd:element name="Item">
<xsd:complexType>
<xsd:element name="Check">
<xsd:complexType>
<xsd:element name="Property">
<xsd:attribute name="id" type="xsd:string" use="required"/>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="value" type="xsd:string" use="required"/>
</xsd:element>
<xsd:attribute name="id" type="xsd:string" use="required"/>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="type" type="xsd:string" use="required"/>
</xsd:attribute>
</xsd:complexType>
</xsd:element>
<xsd:attribute name="id" type="xsd:string" use="required"/>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="effect" type="xsd:string" use="required"/>
</xsd:complexType>
</xsd:element>
</xsd:complexType>
</xsd:element>
</xsd:schema>


C#的验证代码:

        private static bool isValid = true;

static void Main(string[] args)
{
XmlTextReader r = new XmlTextReader("Judge.xml");
XmlValidatingReader v = new XmlValidatingReader(r);

v.ValidationType = ValidationType.Schema;
v.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);

while (v.Read())
{
// Can add code here to process the content.
}
v.Close();

// Check whether the document is valid or invalid.
if (isValid)
Console.WriteLine("Document is valid");
else
Console.WriteLine("Document is invalid");
}

public static void MyValidationEventHandler(object sender, ValidationEventArgs args)
{
isValid = false;
Console.WriteLine("Validation event\n" + args.Message);
}


进行校验的时候总是提示一个System.Xml.Schema.XmlSchemaException
{"The 'http://www.w3.org/2001/XMLSchema:element' element is not supported in this context."}

为什么呀?请指教
...全文
570 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
河西石头 2011-09-07
  • 打赏
  • 举报
回复
关键是vs2010没有帮助文件,加上对校验的步骤没有完整的描述,例子中的写法没有办法应用到自己的额项目中
kousansky 2011-08-05
  • 打赏
  • 举报
回复
我也正在搞这东西,对我来说好难,顶一个.
wocow2316 2008-07-15
  • 打赏
  • 举报
回复
语法有好多错误
如 <xsd:attribute name="id" type="xsd:string" use="required"/>
应该是 <xs:attribute name="id" type="xs:string" use="required"/>

这有问题
<xsd:element name="Property">
<xsd:attribute name="id" type="xsd:string" use="required"/>
<xsd:attribute name="name" type="xsd:string" use="required"/>
<xsd:attribute name="value" type="xsd:string" use="required"/>
</xsd:element>
不能这样写



Macosx 2008-07-15
  • 打赏
  • 举报
回复
你把xsd放到vs里看自然就知道了
gwang119 2008-07-15
  • 打赏
  • 举报
回复
什么地方是非法的呀?
wocow2316 2008-07-15
  • 打赏
  • 举报
回复
用这代码试试

XmlTextReader xtr = null;
XmlValidatingReader xvr = null;
string xmlFile = "../../Judge.xml";//xml源文档
string xsdFile = "../../Judge.xsd";//xsd架构文档
xtr = new XmlTextReader(xmlFile);//构造非验证读取器
XmlSchemaCollection xsc = new XmlSchemaCollection();//构造Schema架构缓存
xsc.Add("http://www.w3.org/2001/XMLSchema.xsd", xsdFile);//在缓存中添加架构文件及对应名字空间
xvr = new XmlValidatingReader(xtr);//构造验证读取器
xvr.Schemas.Add(xsc);//关联验证读取器与架构集合
xvr.ValidationType = ValidationType.Schema;//设置验证类型为Schema架构
xvr.ValidationEventHandler += new ValidationEventHandler(vCallback);
//发生错误时的事件处理程序
while (xvr.Read())//执行读操作
{
}
Console.Write("Finished! " + sign.ToString());
Macosx 2008-07-15
  • 打赏
  • 举报
回复
你那个xsd都是非法的 怎么能验呢
http://msdn.microsoft.com/en-us/library/ms256118.aspx
wocow2316 2008-07-15
  • 打赏
  • 举报
回复
up

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧