用XSD判断XML文件中元素和属性
2006-09-25 14:44
375 查看
据我所知,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;
}
}
}
}
posted on 2005-03-10 22:42 .Net Swimmer 阅读(2331) 评论(3) 编辑 收藏 引用 收藏至365Key 所属分类: ALEIS 设计
评论:
# re: 用XSD判断XML文件中元素和属性 2005-03-22 16:08 | 牛牛
XmlQualifiedName类型是用来干什么的啊??
不是很了解` 回复
# re: 用XSD判断XML文件中元素和属性 2005-07-01 14:40 | longping
<?xml version="1.0" encoding="GB2312"?>
<config>
<field name='ABSCID' title='ABSC编号' inputBox='text' width='120' titleWidth='120'/>
<field name='STOREID' title='库编号' inputBox='text' width='120' titleWidth='120'/>
<field name='UNITEID' title='统一编号' inputBox='text' width='120' titleWidth='120'/>
<field name='ORIGPRODUCINGAREA' title='原产地' inputBox='text' width='120' titleWidth='120'/>
<field name='BREEDAREA' title='繁种地' inputBox='text' width='120' titleWidth='120'/>
<list name='ref'>
<column name='ABSCID'/>
<column name='STOREID'/>
<column name='UNITEID'/>
<column name='ORIGPRODUCINGAREA'/>
<column name='BREEDAREA'/>
</list>
<form name='dialog' title='蚕豆' height='400' width='500'>
<panel cols='2'border='1'>
<item name='ABSCID'/>
<item name='STOREID'/>
<item name='UNITEID'/>
<item name='ORIGPRODUCINGAREA'/>
<item name='BREEDAREA'/>
</panel>
</form>
</config>
回复
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;
}
}
}
}
posted on 2005-03-10 22:42 .Net Swimmer 阅读(2331) 评论(3) 编辑 收藏 引用 收藏至365Key 所属分类: ALEIS 设计
评论:
# re: 用XSD判断XML文件中元素和属性 2005-03-22 16:08 | 牛牛
XmlQualifiedName类型是用来干什么的啊??
不是很了解` 回复
# re: 用XSD判断XML文件中元素和属性 2005-07-01 14:40 | longping
<?xml version="1.0" encoding="GB2312"?>
<config>
<field name='ABSCID' title='ABSC编号' inputBox='text' width='120' titleWidth='120'/>
<field name='STOREID' title='库编号' inputBox='text' width='120' titleWidth='120'/>
<field name='UNITEID' title='统一编号' inputBox='text' width='120' titleWidth='120'/>
<field name='ORIGPRODUCINGAREA' title='原产地' inputBox='text' width='120' titleWidth='120'/>
<field name='BREEDAREA' title='繁种地' inputBox='text' width='120' titleWidth='120'/>
<list name='ref'>
<column name='ABSCID'/>
<column name='STOREID'/>
<column name='UNITEID'/>
<column name='ORIGPRODUCINGAREA'/>
<column name='BREEDAREA'/>
</list>
<form name='dialog' title='蚕豆' height='400' width='500'>
<panel cols='2'border='1'>
<item name='ABSCID'/>
<item name='STOREID'/>
<item name='UNITEID'/>
<item name='ORIGPRODUCINGAREA'/>
<item name='BREEDAREA'/>
</panel>
</form>
</config>
回复
相关文章推荐
- 用XSD判断XML文件中元素和属性
- tomcat配置文件serve.xml的元素及属性详解
- *.hbm.xml映射文件的元素及属性
- 关于xml配置文件无元素提示和the root element of the document is not <xsd:schema>.错误
- .hbm.xml文件中set元素的常用属性
- struts.xml文件中package元素的各大属性讲解
- struts.xml文件中package元素的各大属性讲解
- Hibernate的映射配置文件*.hbm.xml文件中class和property元素的各属性意义和设置
- dom4j 解析元素无属性和有属性的xml文件,都使用反射进行注值
- struts.xml文件中package元素的各大属性讲解
- struts.xml文件中的action元素中的属性及其作用
- Tomcat配置server.xml文件Executor元素属性
- *.hbm.xml映射文件的元素及属性
- dom4j为生成XML的文件添加xmlns属性
- Spring如何加载XSD文件(org.xml.sax.SAXParseException: Failed to read schema document错误的解决方法)
- shell脚本中的逻辑判断 文件目录属性判断 if特殊用法 case判断
- struts2在struts.xml文件中获取或设置action中的属性
- struts.xml配置文件的各个 标签/属性 解析(转)
- C# 使用xsd文件验证XML 格式是否正确
- 最全web.xml配置文件元素详解