例如,如果您有一个名为 NorthwindDataSet 的数据集,与 NorthwindDataSet 中的 DataTable 关联的 TableAdapter 将位于 NorthwindDataSetTableAdapters 命名空间中。要通过编程方法访问特定的 TableAdapter,必须声明 TableAdapter 的新实例。例如:
NorthwindDataSet northwindDataSet = new NorthwindDataSet()
NorthwindDataSetTableAdapters.CustomersTableAdapter customersTableAdapter =
new NorthwindDataSetTableAdapters.CustomersTableAdapter()
customersTableAdapter.Fill(northwindDataSet.Customers)
以下是常用的 TableAdapter 方法和属性:
TableAdapter.Fill
用 TableAdapter 的 SELECT 命令的结果填充 TableAdapter 的关联数据表。有关更多信息,请参见如何:使用数据填充数据集。
TableAdapter.Update
将更改发送回数据库。有关更多信息,请参见如何:使用 TableAdapter 更新数据。
TableAdapter.GetData
返回一个用数据填充了的新 DataTable。
TableAdapter.Insert
在数据表中友圆孝创建新行。有关更多信息,请参见如何:向数据表腔尘中添加行。
TableAdapter.ClearBeforeFill
确定数据表在您调用一个 Fill 方法之前是否被清空。
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. 如何由XML生成XSD?
- 可以用工具,如XMLSpy,首先打开XML, 然芦桐后DTD/Schema ->Generate DTD/Schema, 选择W3c Sehcma即可。
- 此方法不一定能生成确切满足需求的XSD,另需修改。
4. 如何由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)
}
}
}
}
- 反射读取类的属性:
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 == null) return
XmlSerializer xs = new XmlSerializer(typeof(T))
using (StreamWriter sw = new StreamWriter(XMLFileToCreate))
{
xs.Serialize(sw, instance)
}
}
(Link: 使用XMLSerializer类持久化数据 )
这有个.java的验证手高方法修改相应路径后可直接运行验证
package test
import java.io.File
import javax.xml.XMLConstants
import javax.xml.parsers.FactoryConfigurationError
import javax.xml.parsers.ParserConfigurationException
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.DocumentBuilder
import javax.xml.transform.Source
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamSource
import javax.xml.validation.Schema
import javax.xml.validation.SchemaFactory
import javax.xml.validation.Validator
import org.w3c.dom.Document
public class TestDOMValidation
{
public static void main(String[] args)
{
try
{
// Get Document Builder Factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance()
// Leave off validation, and turn off namespaces
factory.setValidating(false)
factory.setNamespaceAware(false)
DocumentBuilder builder = factory.newDocumentBuilder()
Document doc = builder.parse(new File("data/test/shiporder.xml"))
//Document doc = builder.parse(new File("data/test/simple.xml"))
// SchemaFactory is a schema compiler. It reads external representations of schemas and
//毕槐尺 prepares them for validation.
SchemaFactory constraintFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
// Source: an object that implements this interface contains the information needed to act
/明禅/ as source input (XML source or transformation instructions).
Source constraints = new StreamSource(new File("data/test/shiporder.xsd"))
//Source constraints = new StreamSource(new File("data/test/simple.xsd"))
// Schema object represents a set of constraints that can be checked/ enforced against an
// XML document.
Schema schema = constraintFactory.newSchema(constraints)
// Validator is a processor that checks an XML document against Schema.
Validator validator = schema.newValidator()
// Validate the DOM tree
try
{
validator.validate(new DOMSource(doc))
System.out.println("Document validates fine.")
}
catch (org.xml.sax.SAXException e)
{
System.out.println("Validation error: " + e.getMessage())
}
}
catch (ParserConfigurationException e)
{
System.out.println("The underlying parser does not support the requested features.")
}
catch (FactoryConfigurationError e)
{
System.out.println("Error occurred obtaining Document Builder Factory.")
}
catch (Exception e)
{
e.printStackTrace()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)