zoukankan      html  css  js  c++  java
  • 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 " + 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);
  • 相关阅读:
    分享AWS网站
    centos7划分vlan
    在docker容器上如何实现代码的版本管理
    在docker私有仓库如何查看有哪些镜像?
    centos7下报错: import requests ImportError: No module named requests
    Unity2018.4.7导出Xcode工程报错解决方案
    1.OpenGL mac开发环境搭建记录
    unity 模板测试 详解
    游戏战争迷雾
    Unity 移动平台自己编写Shader丢失问题
  • 原文地址:https://www.cnblogs.com/baozhu/p/5064897.html
Copyright © 2011-2022 走看看