如果数据集为空,如何将xml加载到数据集
本文关键字:数据集 xml 加载 如果 | 更新日期: 2023-09-27 18:11:17
我有一个数据集,它是在通过我的webservice执行存储过程后引入的。如果数据集为空,我需要返回一个通用结果,而不是什么都不返回。返回的XML,在我的WebService (.ASMX)代码下面,是吐出的.XML,如果它是EMPTY....不确定这是否有帮助,但我完全不知所措。理想情况下,我想看看Table1是否存在,然后执行一些操作....但我还是很困惑。谢谢!
public DataSet GetMaleDogsDS()
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
//Define the dataset, in this case dsDogs
DataSet dsDogs = new DataSet();
//
dsDogs.Tables.Add("Dogs");
DbCommand dbCommand = null;
JavaScriptSerializer js = new JavaScriptSerializer();
try
{
Database db = DatabaseFactory.CreateDatabase();
string sqlCommand = "sp_GetDogs";
dbCommand = db.GetSqlStringCommand(sqlCommand);
dbCommand.CommandType = CommandType.StoredProcedure;
db.AddInParameter(dbCommand, "Gender", DbType.String, "male");
dsDogs = db.ExecuteDataSet(dbCommand);
string strXml = dsDogs.GetXml();
return dsDogs;
}
catch
{
string strXml = js.Serialize(dsDogs);
return dsDogs;
}
}
下面是EMPTY结果的XML:
<DataSet xmlns="http://0mitted.net/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="NewDataSet">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="Table">
<xs:complexType>
<xs:sequence>
<xs:element name="DogID" type="xs:string" minOccurs="0"/>
<xs:element name="LongDesc" type="xs:string" minOccurs="0"/>
<xs:element name="ShortDesc" type="xs:string" minOccurs="0"/>
<xs:element name="ThumbURL" type="xs:string" minOccurs="0"/>
<xs:element name="LargeURL" type="xs:string" minOccurs="0"/>
<xs:element name="Gender" type="xs:string" minOccurs="0"/>
<xs:element name="Age" type="xs:string" minOccurs="0"/>
<xs:element name="Weight" type="xs:string" minOccurs="0"/>
<xs:element name="HealthStatus" type="xs:string" minOccurs="0"/>
<xs:element name="ActivityLevel" type="xs:string" minOccurs="0"/>
<xs:element name="PersonalityTraits" type="xs:string" minOccurs="0"/>
<xs:element name="InteractionDog" type="xs:string" minOccurs="0"/>
<xs:element name="InteractionCat" type="xs:string" minOccurs="0"/>
<xs:element name="ChildRequirement" type="xs:string" minOccurs="0"/>
<xs:element name="FenceRequirement" type="xs:string" minOccurs="0"/>
<xs:element name="WorkSchedule" type="xs:string" minOccurs="0"/>
<xs:element name="LeashManners" type="xs:string" minOccurs="0"/>
<xs:element name="TrainingNeeded" type="xs:string" minOccurs="0"/>
<xs:element name="AdoptionStatus" type="xs:string" minOccurs="0"/>
<xs:element name="AdoptionFee" type="xs:string" minOccurs="0"/>
<xs:element name="Name" type="xs:string" minOccurs="0"/>
<xs:element name="behavior" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"/>
</DataSet>
您可以使用此代码检查数据集是否为空,如果此条件为真,则加载xml文件到数据集:
if (ds.Tables[0].Rows.Count == 0)
{
DataSet dataSet = new DataSet();
dataSet.ReadXml("input.xml", XmlReadMode.ReadSchema);
//...
}
在您的场景中,这段代码应该像这样:
dsDogs = db.ExecuteDataSet(dbCommand);
if (dsDogs.Tables[0].Rows.Count == 0)
{
dsDogs.ReadXml("empty.xml", XmlReadMode.ReadSchema);
}
return dsDogs;