asp.net如何使用xml schema

dream__life 2010-07-03 06:44:11
其实我也刚接触xml,xml schema应该是用来验证XML的吧?

现在我有一个xml schema文件,是客户提供的一个接口标准,要生成符合条件的XML文件。

如果我现在在程序中取得一些数据,并生成了一个XML文件,如何使用那个xml schema进行验证?

...全文
131 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
直男250 2010-07-05
  • 打赏
  • 举报
回复
schema依你描述的意思应该是数据格式定义了。
那么,考虑以下情况:
1.DataSet带有ToSchema属性(或者ReadSchema?),忘记了,自己在开发环境去按"."吧,好久不代码了。
2.楼主要验证格式,可以通过反射。
3.或者直接用DataSet读schema文件,取到数据格式后,也就是一个标准的带有字段、唯一性、主键等等数据集格式。
4.与你取得的shema结构做个对比,MARGE也好,循环也罢。

bychgh 2010-07-05
  • 打赏
  • 举报
回复
帮顶~~~~~~~
gongsun 2010-07-05
  • 打赏
  • 举报
回复
...
qiqishardgel 2010-07-05
  • 打赏
  • 举报
回复
学习,帮顶!
nextuntil 2010-07-04
  • 打赏
  • 举报
回复
2L的厉害,顶一个。、
HolyPlace 2010-07-03
  • 打赏
  • 举报
回复
参考

using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;

public class Sample
{

public static void Main ()
{
//Load the schema collection.
XmlSchemaCollection xsc = new XmlSchemaCollection();
xsc.Add( "urn:bookstore-schema ", "books.xsd ");

//Validate the file using the schema stored in the collection.
//Any elements belonging to the namespace "urn:cd-schema " generate
//a warning since the there is no schema matching that namespace.
Validate( "store.xml ", xsc);
}

private static void Validate(String filename, XmlSchemaCollection xsc)
{
Console.WriteLine();
Console.WriteLine( "\r\nValidating XML file {0}... ", filename.ToString());
XmlTextReader reader = new XmlTextReader (filename);
XmlValidatingReader vreader=new XmlValidatingReader (reader);
vreader.ValidationType = ValidationType.Schema;
vreader.Schemas.Add(xsc);

//Set the validation event handler.
vreader.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack);
//Read the XML data.
while (vreader.Read()){}

//Close the reader.
vreader.Close();
}

//Display any warnings or errors.
public static void ValidationCallBack (object sender, ValidationEventArgs args)
{
if (args.Severity==XmlSeverityType.Warning)
Console.WriteLine( "\tWarning: Matching schema not found. No validation occurred. " + args.Message);
else
Console.WriteLine( "\tValidation error: " + args.Message);

}
}

小_虎 2010-07-03
  • 打赏
  • 举报
回复

62,041

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

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