zoukankan      html  css  js  c++  java
  • xmlsechema验证

                   //创建xmlDocument 

                   XmlDocument doc = new XmlDocument(); 
       
                    //创建声明段 如<?xml version="1.0" encoding="utf-8" ?>  
                    doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null)); 
                    //创建一个根节点 KYTResults 
                    XmlElement results = doc.CreateElement("KYTResults"); 
       
                    //创建 ResultsStatus 
                    XmlNode resultsStatus = doc.CreateElement("ResultsStatus"); 
                    //创建Level 
                    XmlElement element = doc.CreateElement("Level"); 
                    element.InnerText = status ? "0" : "1"
                    resultsStatus.AppendChild(element); 
                    //创建Description 
                    element = doc.CreateElement("Description"); 
                    element.InnerText = msg; 
                    resultsStatus.AppendChild(element); 
                    //创建PassKey 如果用户登录失败 将加载空字符 
                    element = doc.CreateElement("PassKey"); 
                    element.InnerText = key; 
                    resultsStatus.AppendChild(element); 
                    results.AppendChild(resultsStatus); 
                    //END 创建 ResultsStatus  
       
                    //创建 DataList  数据集合 
                    if (status) 
                    
                        results.AppendChild(dataList); 
                    
                    //END 创建一个根节点 KYTResults 
                    doc.AppendChild(results); 
       
       
                    string path = Server.MapPath("/ws/xsd/ReceiveReturn.xsd"); 
       
    //验证xml格式是否正确 通过xsd验证 
                        string error = ""
                        //声明XmlSchema 
                        XmlSchemaSet schemas = new XmlSchemaSet(); 
                        schemas.Add("", XmlReader.Create(path)); 
                        //声明事件处理方法 
                        ValidationEventHandler eventHandler = new ValidationEventHandler(delegate(object sender, ValidationEventArgs e) { 
                            switch (e.Severity) 
                            
                                case XmlSeverityType.Error: 
                                    error += e.Message; 
                                    break
                                case XmlSeverityType.Warning: 
                                    break
                            
                        }); 
                        doc.Schemas = schemas; 
                        //验证xml 
                        doc.Validate(eventHandler); 
                        //检查是否有异常 如果格式不正确就抛出来 
                        if (!"".Equals(error)) 
                        
                            throw new Exception(error); 
                        
  • 相关阅读:
    乘法相关
    hdu 1211 RSA (逆元)
    hdu 1811 Rank of Tetris (拓扑 & 并查集)
    hdu 2153 仙人球的残影
    hdu 1426 Sudoku Killer (dfs)
    hdu 2510 符号三角形 (DFS+打表)
    zoj 1002 Fire Net (二分匹配)
    hdu 1172 猜数字
    hdu 1728 逃离迷宫 (bfs)
    《Effective C++》笔记:III(转载)
  • 原文地址:https://www.cnblogs.com/wcLT/p/3956240.html
Copyright © 2011-2022 走看看