2014年8月25日 星期一

DataSet 轉 XML (xml,xsd)

DataTable轉XML
DataTable dt = new DataTable("WPN");
dt.Columns.AddRange(new DataColumn[] {
   //new DataColumn("JCN",typeof(string),"",MappingType.Attribute),
   new DataColumn("ID1",typeof(string)),
   new DataColumn("ID2",typeof(string)),
   new DataColumn("NO",typeof(int)),
   new DataColumn("DT",typeof(DateTime))
  });

  dt.Columns["JCN"].MaxLength= 15;
  dt.Columns["MSNM"].MaxLength = 80;
           
  DateTime x = DateTime.Now;
  dt.PrimaryKey = new DataColumn[] { dt.Columns["ID1"], dt.Columns["ID2"]};

  dt.Rows.Add(new object[] { "WP001", "WP002", 3, Convert.ToDateTime("2014/06/25 10:16:08")});

  DataSet ds = new DataSet("WP");
  ds.Tables.Add(dt);

  ds.WriteXml("D:\\test.xml");
  ds.WriteXmlSchema("D:\\test.xsd");
驗證XSD
http://msdn.microsoft.com/en-us/library/3740e0b5(v=vs.110).aspx
string xmlFile = Server.MapPath("D:\\test.xml");
string xsdFile = Server.MapPath("D:\\test.xsd");

XmlTextReader textReader = new XmlTextReader(xmlFile);
XmlValidatingReader validatingReader = new XmlValidatingReader(textReader);
validatingReader.Schemas.Add(null, xsdFile);
validatingReader.ValidationType = ValidationType.Schema;
validatingReader.ValidationEventHandler += new ValidationEventHandler(validatingReader_ValidationEventHandler);

while (validatingReader.Read()){

}
validatingReader.Close();
public void validatingReader_ValidationEventHandler(object sender, ValidationEventArgs e)
{
   Response.Write("Error Message : " + e.Message);
}

沒有留言:

張貼留言