如何在DataTable中预定义XML模式 [英] How to predefine an XML schema in a DataTable

查看:59
本文介绍了如何在DataTable中预定义XML模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello everybodyA



我想为DataTable对象预定义XML模式。仅此一点没有问题。

但是我如何强制ReadXML方法(或类似方法)坚持这种模式,而不是仅仅用XML文件的模式替换它?当文件中的模式不同时,应该有异常或者只是没有加载数据。

如果代码最终有效,那么代码如何看起来如何? XML文件和DataTable的所有约束和元素都必须相同吗?



提前非常感谢!



样本文件:

Hello everybodyA

I want to predefine an XML schema for a DataTable object. This alone is no problem.
But how can i force the ReadXML method (or similar) to stick to this schema, instead of just replacing it by the XML file's schema? When the schema in the file is different there should be an exception or just no loading of data.
How would that code look like exactly, if it works in the end? Do all contraints and elements of the XML file and the DataTable have to be the same?

Thank you very much in advance!

sample file:

<?xml version="1.0" standalone="yes"?>
<Parameter_x0020_File>
 <xs:schema id="Parameter_x0020_Files" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="Parameter_x0020_Files" msdata:IsDataSet="true" msdata:MainDataTable="Tools" msdata:UseCurrentLocale="true">
   <xs:complexType>
    <xs:choice minOccurs="0" maxOccurs="unbounded">
     <xs:element name="Tools">
      <xs:complexType>
       <xs:sequence>
        <xs:element name="ID" msdata:ReadOnly="true" type="xs:string" />
         <xs:element name="Name" type="xs:string" minOccurs="0" />
       </xs:sequence>
      </xs:complexType>
     </xs:element>
    </xs:choice>
   </xs:complexType>
  </xs:element>
 </xs:schema>
 <Tools>
  <ID>10</ID>
  <Name>sample</Name>
 </Tools>
</Parameter_x0020_File>

推荐答案

如果在加载和抛出之前使用XSD验证XML,会不会更容易如果失败会出错吗?



我可能会遗漏一些东西..请告诉我这种方法是否有效



Wouldn't it be easier if you validated your XML with XSD before loading and throw an error if it fails ?

I might be missing something here..please let me know if that approach works

XmlSchemaSet mySch= new XmlSchemaSet();
mySch.Add(SCH_NAMESPACE, YOUR_SCHEMA_FILE);

XDocument doc = XDocument.Load(YOU_XML_FILENAME);
string errMsg = "";
doc.Validate(mySch, (o, e) => {
    msg += e.Message + Environment.NewLine;
});
if(msg != "")
{
//handle error here
}
else
{
//load datatable
}


这篇关于如何在DataTable中预定义XML模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆