现在的位置: 首页 > 综合 > 正文

如何由XSD自动生成XML和实体类(zz)

2013年05月09日 ⁄ 综合 ⁄ 共 5512字 ⁄ 字号 评论关闭
项目中有时候要用XML作为数据源,因此需要定义XML文件和相应的类,最佳方法是首先定义XSD,然后自动生成实体类,最后生成XML和填充数据;读取XML数据源的时候,首先用XSD验证XML数据格式,然后自动读入实体类使用。

定义XSD的过程就是类似设计表结构的过程,二者也可以相互转化。

本文讨论了如何定义XSD文件(XML Schema),然后自动生成实体类,最后生成XML的过程,以及如何根据XSD验证XML文件的合法性。以供大家做项目时参考。

1. 首先在VS2005中添加一个XSD文件。

2. 使用VS2005工具XSD.exe(SDK"v2.0"Bin"xsd.exe)自动生成实体类:

xsd /c /namespace:myCompany /language:CS temp1.xsd

也可以生成DataSet类型的类:

xsd /dataset /language:CS temp1.xsd

 ( 类文件和XSD之间可以相互转换,也就是说,你也可以先生成类,然后自动生成XSD)

    自动读取XML数据到实体类:

XmlSerializer xs = new XmlSerializer(typeof(myClassType));
using (FileStream fs = new FileStream(XmlFilePath, FileMode.Open))
{
    
return (myClassType)xs.Deserialize(fs);
}

3. 如何由XSD生成XML?

   - 可以用其他工具,如XMLSpy,DTD/Schema -> Generate sample XML file...
  
   - 可以由XSD生成类,然后写代码实例化这个类,最后序列化为XML

   - 如何自动给类每个属性设置一个空值:(用反射的方法)

     代码示例:

/// <summary>
        
/// Get all properties and set default value
        
/// </summary>
        
/// <typeparam name="T">Type</typeparam>
        
/// <param name="item">Object</param>

        private static void ReflctProperties<T>(T item)
        
{
            PropertyInfo[] pty 
= typeof(T).GetProperties();
            Type t 
= item.GetType();

            
if (pty != null)
            
{
                
foreach (PropertyInfo info in pty)
                
{
                    
if (!info.CanWrite) continue;

                    
if (info.PropertyType == typeof(String))
                    
{
                        t.GetProperty(info.Name).SetValue(item, String.Empty, 
null);
                    }

                    
if (info.PropertyType == typeof(Boolean))
                    
{
                        t.GetProperty(info.Name).SetValue(item, 
truenull);
                    }

                }

            }

        }

 

    - 反射读取类的属性:
 

public static object GetProperty<T>(T item, string PropertyName)
{
     PropertyInfo propertyInfo 
= item.GetType().GetProperty(PropertyName);
     
if (propertyInfo != null)
     
{                
         
return propertyInfo.GetValue(item, null);
     }

     
return null;
}

   - 如何序列化为XML?

/**//// <summary>
        
/// Serialize class instance to XML file
        
/// </summary>
        
/// <typeparam name="T">type</typeparam>
        
/// <param name="XMLFileToCreate">XMLFileToCreate</param>
        
/// <param name="instance">class instance</param>

        public void Serialize<T>(string XMLFileToCreate, T instance)
        
{
            
if (instance == nullreturn;

            XmlSerializer xs 
= new XmlSerializer(typeof(T));
            
using (StreamWriter sw = new StreamWriter(XMLFileToCreate))
            
{
                xs.Serialize(sw, instance);
            }

        }

4. 如何使用XSD来验证XML文件合法性:
  
   - 使用XMLSpy,首先Assign XSD,然后验证 (其实就是设置XML里面引用的schema,注意schema可能引用其他的schema)
 
   - 代码中验证:

   Validate XML against XSDValidate XML against XSD

from :
http://www.cnblogs.com/Mainz/archive/2007/12/16/996857.html

项目中有时候要用XML作为数据源,因此需要定义XML文件和相应的类,最佳方法是首先定义XSD,然后自动生成实体类,最后生成XML和填充数据;读取XML数据源的时候,首先用XSD验证XML数据格式,然后自动读入实体类使用。

定义XSD的过程就是类似设计表结构的过程,二者也可以相互转化。

本文讨论了如何定义XSD文件(XML Schema),然后自动生成实体类,最后生成XML的过程,以及如何根据XSD验证XML文件的合法性。以供大家做项目时参考。

1. 首先在VS2005中添加一个XSD文件。

2. 使用VS2005工具XSD.exe(SDK"v2.0"Bin"xsd.exe)自动生成实体类:

xsd /c /namespace:myCompany /language:CS temp1.xsd

也可以生成DataSet类型的类:

xsd /dataset /language:CS temp1.xsd

 ( 类文件和XSD之间可以相互转换,也就是说,你也可以先生成类,然后自动生成XSD)

    自动读取XML数据到实体类:

XmlSerializer xs = new XmlSerializer(typeof(myClassType));
using (FileStream fs = new FileStream(XmlFilePath, FileMode.Open))
{
    
return (myClassType)xs.Deserialize(fs);
}

3. 如何由XSD生成XML?

   - 可以用其他工具,如XMLSpy,DTD/Schema -> Generate sample XML file...
  
   - 可以由XSD生成类,然后写代码实例化这个类,最后序列化为XML

   - 如何自动给类每个属性设置一个空值:(用反射的方法)

     代码示例:

/// <summary>
        
/// Get all properties and set default value
        
/// </summary>
        
/// <typeparam name="T">Type</typeparam>
        
/// <param name="item">Object</param>

        private static void ReflctProperties<T>(T item)
        
{
            PropertyInfo[] pty 
= typeof(T).GetProperties();
            Type t 
= item.GetType();

            
if (pty != null)
            
{
                
foreach (PropertyInfo info in pty)
                
{
                    
if (!info.CanWrite) continue;

                    
if (info.PropertyType == typeof(String))
                    
{
                        t.GetProperty(info.Name).SetValue(item, String.Empty, 
null);
                    }

                    
if (info.PropertyType == typeof(Boolean))
                    
{
                        t.GetProperty(info.Name).SetValue(item, 
truenull);
                    }

                }

            }

        }

 

    - 反射读取类的属性:
 

public static object GetProperty<T>(T item, string PropertyName)
{
     PropertyInfo propertyInfo 
= item.GetType().GetProperty(PropertyName);
     
if (propertyInfo != null)
     
{                
         
return propertyInfo.GetValue(item, null);
     }

     
return null;
}

   - 如何序列化为XML?

/**//// <summary>
        
/// Serialize class instance to XML file
        
/// </summary>
        
/// <typeparam name="T">type</typeparam>
        
/// <param name="XMLFileToCreate">XMLFileToCreate</param>
        
/// <param name="instance">class instance</param>

        public void Serialize<T>(string XMLFileToCreate, T instance)
        
{
            
if (instance == nullreturn;

            XmlSerializer xs 
= new XmlSerializer(typeof(T));
            
using (StreamWriter sw = new StreamWriter(XMLFileToCreate))
            
{
                xs.Serialize(sw, instance);
            }

        }

4. 如何使用XSD来验证XML文件合法性:
  
   - 使用XMLSpy,首先Assign XSD,然后验证 (其实就是设置XML里面引用的schema,注意schema可能引用其他的schema)
 
   - 代码中验证:

   Validate XML against XSDValidate XML against XSD

from :
http://www.cnblogs.com/Mainz/archive/2007/12/16/996857.html

抱歉!评论已关闭.