惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Help Net Security
博客园 - Franky
GbyAI
GbyAI
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
爱范儿
爱范儿
IT之家
IT之家
酷 壳 – CoolShell
酷 壳 – CoolShell
aimingoo的专栏
aimingoo的专栏
博客园_首页
MongoDB | Blog
MongoDB | Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
Recent Announcements
Recent Announcements
Scott Helme
Scott Helme
有赞技术团队
有赞技术团队
M
MIT News - Artificial intelligence
C
CERT Recently Published Vulnerability Notes
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Jina AI
Jina AI
F
Fortinet All Blogs
N
Netflix TechBlog - Medium
L
LangChain Blog
L
LINUX DO - 最新话题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
H
Hacker News: Front Page
MyScale Blog
MyScale Blog
P
Palo Alto Networks Blog
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
AI
AI
T
Troy Hunt's Blog
Microsoft Azure Blog
Microsoft Azure Blog
阮一峰的网络日志
阮一峰的网络日志
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Vercel News
Vercel News
Microsoft Security Blog
Microsoft Security Blog
罗磊的独立博客
S
Secure Thoughts
大猫的无限游戏
大猫的无限游戏
博客园 - 叶小钗
人人都是产品经理
人人都是产品经理
Blog — PlanetScale
Blog — PlanetScale
博客园 - 司徒正美
Apple Machine Learning Research
Apple Machine Learning Research
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Security @ Cisco Blogs
Cloudbric
Cloudbric
E
Exploit-DB.com RSS Feed
Attack and Defense Labs
Attack and Defense Labs

博客园 - .3S Swimmer

用类的方式实现资源国际化 CVS使用汇编 CVS使用中遇到的问题及解决办法[随记] - .3S Swimmer - 博客园 在.net 2005 环境中开发ArcMap工具条 ArcIMS开发(一)——ArcIMS API 简介 使用ArcIMS9.2+Java ADF建立基本的WebGIS站点的尝试 从已有的文件中恢复ClearCase LT Server 申请了一个gmail邮箱,体验ing C#中创建和使用资源动态链接库 C#中unhandled异常处理的问题 解决SQL Server 连接时的一些基本问题后的若干初浅心得 使用XML的DOM和XPath来创建多项选择题的在线测试 二维数据表的显示 农用地定级系统工作计划 二维数据表的XML描述 ALEIS,啊,累死 我不认为我喜欢编程 ClearCase LT的使用心得(二) ALEIS定级部分工作分配
用XSD判断XML文件中元素和属性
.3S Swimmer · 2005-03-10 · via 博客园 - .3S Swimmer

        据我所知,ADO.Net好像不能实现,用XSD(Schema)判断读取节点的数据类型的功能,而是将所有的数据都作为string。下面这个类用xsd判断元素或属性的数据类型:

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

namespace REIT.ALEIS.Xml
{
 /// <summary>
 /// ReitXmlDataValidator 从一个xsd文件中查找
 /// 某个元素或属性的数据类型,并将它转换为.Net的数据类型
 /// </summary>
 public class ReitXmlDataTypeFinder
 {
  private XmlSchema m_Schema;

  public ReitXmlDataTypeFinder(string XSDFilePath)
  {
   FileStream fs;
   try
   {
    fs = new FileStream(XSDFilePath, FileMode.Open);
    m_Schema = XmlSchema.Read(fs,new ValidationEventHandler(ShowCompileError));
    m_Schema.Compile(new ValidationEventHandler(ShowCompileError));//这里一定要编译一次
   }
   catch
   {
    throw;
    }
  }

  public System.Type FindElementType(string ElementName)
  {
   System.Xml.XmlQualifiedName Name = new XmlQualifiedName(ElementName,@"http://www.reitweb.com/fc");
   XmlSchemaObject obj = m_Schema.Elements[Name];
   if(obj==null) return null;

   return FindType(obj);
  }
   
  public System.Type FindAttributeType(string AttributeName)
  {
   System.Xml.XmlQualifiedName AttrName = new XmlQualifiedName(AttributeName,@"http://www.reitweb.com/fc");

   XmlSchemaObject obj = m_Schema.Attributes[AttrName];
   if(obj==null) return null;

   if (obj.GetType() != typeof(XmlSchemaAttribute))
    return null;
   
   XmlSchemaAttribute attr = (XmlSchemaAttribute)obj;
   if(attr.AttributeType!=null)
    return FindType(attr.AttributeType);
   else
    if(attr.SchemaTypeName.Name.CompareTo("anyType")==0) return typeof(object);           
     
   return null;
  }

  private static void ShowCompileError(object sender, ValidationEventArgs e)
  {
   Console.WriteLine("Validation Error: {0}", e.Message);
  }

  private System.Type FindType(object obj)
  {
   if(obj.GetType()==typeof(XmlSchemaSimpleType))
   {
    XmlSchemaSimpleType simpleType = (XmlSchemaSimpleType)obj;
    return simpleType.Datatype.ValueType;
   }
   else if(obj.GetType()==typeof(XmlSchemaComplexType))
   {
    XmlSchemaComplexType complexType = (XmlSchemaComplexType)obj;
    return complexType.Datatype.ValueType;
   }
   else
   {
    XmlSchemaDatatype datatype = (XmlSchemaDatatype)obj;
    return datatype.ValueType;
   }   
  }
 }
}