xsd操作

1.xsd介绍
详见: http://blog.sina.com.cn/s/blog_ad0672d60102uy6w.html
2.生成xsd
DataSet dataSet = new DataDet();
// read date from xml file
dataSet.ReadXml(@"xml2.xml", XmlReadMode.ReadSchema);
// .. or set data with code
// save as xsd file
System.IO.StreamWriter writer = new System.IO.StreamWriter("Customers.xsd");
dataSet.WriteXmlSchema(writer);
writer.Close();
// get xsd format contents
string schemaString = dataSet.GetXmlSchema();
// save as xml
dataSet.WriteXml(@"xml2_new.xml", XmlWriteMode.WriteSchema);
3.由xsd生成xml
xsd.exe工具可生成操作的cs类,再有生成的cs类生成xml文件
详见: http://blog.sina.com.cn/s/blog_ad0672d60101g02h.html
4.xsd检验xml
using System.Xml;        // for XmlTextReader and XmlValidatingReader
using System.Xml.Schema; // for XmlSchemaCollection (which is used later)
private static bool isValid = true;      // If a validation error occurs,
                                         // set this flag to false in the
                                         // validation event handler. 
XmlTextReader r = new XmlTextReader("ProductWithXSD.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);
}
// 另一种方法
XDocument xDoc = XDocument.Parse(xxxxxxx);
string xsdPath = ConfigUtils.GetXsdPath(XsdSchemaIdentifier.HHDataItem);
XmlSchemaSet xss = new XmlSchemaSet();
xss.Add("", xsdPath);
fs.Close();
fs.Dispose();
xDoc.Validate(xss, null);

转载于:https://www.cnblogs.com/baozhu/p/5064897.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值