定义XSD的过程就是类似设计表结构的过程,二者也可以相互转化。
本文讨论了如何定义XSD文件(XML Schema),然后自动生成实体类,最后生成XML的过程,以及如何根据XSD验证XML文件的合法性。以供大家做项目时参考。
1. 首先在 VS2005中添加一个XSD文件。例子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
自动读取XML数据到实体类:
XmlSerializer xs
=
new
XmlSerializer(
typeof
(myClassType));
using (FileStream fs = new FileStream(XmlFilePath, FileMode.Open))
{
return (myClassType)xs.Deserialize(fs);
}
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, true, null);
}
}
}
}
/// 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, true, null);
}
}
}
}
- 反射读取类的属性:
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;
}
{
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 == null) return;
XmlSerializer xs = new XmlSerializer(typeof(T));
using (StreamWriter sw = new StreamWriter(XMLFileToCreate))
{
xs.Serialize(sw, instance);
}
}
/// 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 == null) return;
XmlSerializer xs = new XmlSerializer(typeof(T));
using (StreamWriter sw = new StreamWriter(XMLFileToCreate))
{
xs.Serialize(sw, instance);
}
}
- 使用XMLSpy,首先Assign XSD,然后验证 (其实就是设置XML里面引用的schema,注意schema可能引用其他的schema)
- 代码中验证:
Validate XML against XSD