上面一些声明性的代码都作了删节,另外几个涉及到的方法的代码如下:
private void CreateFakeData()
{
DataRow row;
//deal with PsgInfo table, create two rows
row = dataSet1.Tables["PsgInfo"].NewRow();
row["PsgId"] = 1;
row["PsgName"] = "李承民";
row["FltNo"] = "MU501";
row["FltDate"] = "2004-3-2";
row["SeatNo"] = "10A";
row["BrdNo"] = "022";
row["Contact"] = "021-51136111";
row["BrdPass"] = "MU501 02 HKG022";
row["CkiCounter"] = "A03";
row["CkiTime"] = "2004-03-02 15:12:38";
dataSet1.Tables["PsgInfo"].Rows.Add(row); row = dataSet1.Tables["PsgInfo"].NewRow();
row["PsgId"] = 2;
row["PsgName"] = "刘丽";
row["FltNo"] = "MU501";
row["FltDate"] = "2004-3-2";
row["SeatNo"] = "10B";
row["BrdNo"] = "023";
row["Contact"] = "021-51136111";
row["BrdPass"] = "MU501 02 HKG023";
row["CkiCounter"] = "A03";
row["CkiTime"] = "2004-03-02 15:15:38";
dataSet1.Tables["PsgInfo"].Rows.Add(row);
//deal with BagInfo table,create one row for PsgId=1
row = dataSet1.Tables["BagInfo"].NewRow();
row["BagId"] = 1;
row["BagTag"] = "0781123456";
row["CkiTime"] = "2004-03-02 15:12:38";
row["Channel"] = "A03F";
row["State"] = "开包";
row["PsgId"] = 1;
dataSet1.Tables["BagInfo"].Rows.Add(row); //deal with BagChk table, create two check record for BagTag=0781123456
row = dataSet1.Tables["BagChk"].NewRow();
row["ChkId"] = 1;
row["ForbidType"] = "管制刀具";
row["Amount"] = 2;
row["DealType"] = "暂存";
row["BagId"] = 1;
dataSet1.Tables["BagChk"].Rows.Add(row); row = dataSet1.Tables["BagChk"].NewRow();
row["ChkId"] = 2;
row["ForbidType"] = "毒品";
row["Amount"] = 1;
row["DealType"] = "上报";
row["BagId"] = 1;
dataSet1.Tables["BagChk"].Rows.Add(row); dataSet1.AcceptChanges();
}
/// <summary>
/// 将开包的结果保存在XML文件中.注意,这里的文件名和路径都是Hard-coded的.
/// </summary>
private void SaveResultToXml(string filename)
{
FileStream fs;
fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
// Create an XmlTextWriter using the file stream.
XmlTextWriter xtw = new XmlTextWriter(fs, Encoding.Unicode); try
{
// Write the XML to the file.
dataSet1.WriteXml(xtw, XmlWriteMode.WriteSchema);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
xtw.Close( );
} }
/// <summary>
/// 将数据从XML文件中load回来。
/// </summary>
public void LoadResultFromXml(string filename)
{
FileStream fs;
fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
// Create an XmlTextReader using the file stream.
XmlTextReader xtr = new XmlTextReader(fs);
try
{
// Read from XML file.
dataSet1.ReadXml(xtr, XmlReadMode.ReadSchema);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
xtr.Close( );
} }
private void CreateFakeData()
{
DataRow row;
//deal with PsgInfo table, create two rows
row = dataSet1.Tables["PsgInfo"].NewRow();
row["PsgId"] = 1;
row["PsgName"] = "李承民";
row["FltNo"] = "MU501";
row["FltDate"] = "2004-3-2";
row["SeatNo"] = "10A";
row["BrdNo"] = "022";
row["Contact"] = "021-51136111";
row["BrdPass"] = "MU501 02 HKG022";
row["CkiCounter"] = "A03";
row["CkiTime"] = "2004-03-02 15:12:38";
dataSet1.Tables["PsgInfo"].Rows.Add(row); row = dataSet1.Tables["PsgInfo"].NewRow();
row["PsgId"] = 2;
row["PsgName"] = "刘丽";
row["FltNo"] = "MU501";
row["FltDate"] = "2004-3-2";
row["SeatNo"] = "10B";
row["BrdNo"] = "023";
row["Contact"] = "021-51136111";
row["BrdPass"] = "MU501 02 HKG023";
row["CkiCounter"] = "A03";
row["CkiTime"] = "2004-03-02 15:15:38";
dataSet1.Tables["PsgInfo"].Rows.Add(row);
//deal with BagInfo table,create one row for PsgId=1
row = dataSet1.Tables["BagInfo"].NewRow();
row["BagId"] = 1;
row["BagTag"] = "0781123456";
row["CkiTime"] = "2004-03-02 15:12:38";
row["Channel"] = "A03F";
row["State"] = "开包";
row["PsgId"] = 1;
dataSet1.Tables["BagInfo"].Rows.Add(row); //deal with BagChk table, create two check record for BagTag=0781123456
row = dataSet1.Tables["BagChk"].NewRow();
row["ChkId"] = 1;
row["ForbidType"] = "管制刀具";
row["Amount"] = 2;
row["DealType"] = "暂存";
row["BagId"] = 1;
dataSet1.Tables["BagChk"].Rows.Add(row); row = dataSet1.Tables["BagChk"].NewRow();
row["ChkId"] = 2;
row["ForbidType"] = "毒品";
row["Amount"] = 1;
row["DealType"] = "上报";
row["BagId"] = 1;
dataSet1.Tables["BagChk"].Rows.Add(row); dataSet1.AcceptChanges();
}
/// <summary>
/// 将开包的结果保存在XML文件中.注意,这里的文件名和路径都是Hard-coded的.
/// </summary>
private void SaveResultToXml(string filename)
{
FileStream fs;
fs = new FileStream(filename, FileMode.Create, FileAccess.Write);
// Create an XmlTextWriter using the file stream.
XmlTextWriter xtw = new XmlTextWriter(fs, Encoding.Unicode); try
{
// Write the XML to the file.
dataSet1.WriteXml(xtw, XmlWriteMode.WriteSchema);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
xtw.Close( );
} }
/// <summary>
/// 将数据从XML文件中load回来。
/// </summary>
public void LoadResultFromXml(string filename)
{
FileStream fs;
fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
// Create an XmlTextReader using the file stream.
XmlTextReader xtr = new XmlTextReader(fs);
try
{
// Read from XML file.
dataSet1.ReadXml(xtr, XmlReadMode.ReadSchema);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
xtr.Close( );
} }
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:Locale="zh-CN">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="PsgInfo">
<xs:complexType>
<xs:sequence>
<xs:element name="PsgName" type="xs:string" minOccurs="0" />
<xs:element name="FltNo" type="xs:string" minOccurs="0" />
<xs:element name="FltDate" type="xs:string" minOccurs="0" />
<xs:element name="SeatNo" type="xs:string" minOccurs="0" />
<xs:element name="BrdNo" type="xs:string" minOccurs="0" />
<xs:element name="Contact" type="xs:string" minOccurs="0" />
<xs:element name="BrdPass" type="xs:string" minOccurs="0" />
<xs:element name="CkiCounter" type="xs:string" minOccurs="0" />
<xs:element name="CkiTime" type="xs:string" minOccurs="0" />
<xs:element name="PsgId" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="BagInfo">
<xs:complexType>
<xs:sequence>
<xs:element name="BagTag" type="xs:string" />
<xs:element name="CkiTime" type="xs:string" minOccurs="0" />
<xs:element name="Channel" type="xs:string" minOccurs="0" />
<xs:element name="State" type="xs:string" minOccurs="0" />
<xs:element name="PsgId" type="xs:int" minOccurs="0" />
<xs:element name="BagId" type="xs:int" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="BagChk">
<xs:complexType>
<xs:sequence>
<xs:element name="ChkId" type="xs:int" />
<xs:element name="ForbidType" type="xs:string" minOccurs="0" />
<xs:element name="Amount" type="xs:string" minOccurs="0" />
<xs:element name="DealType" type="xs:string" minOccurs="0" />
<xs:element name="BagId" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
<xs:unique name="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//PsgInfo" />
<xs:field xpath="PsgId" />
</xs:unique>
<xs:unique name="BagInfo_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//BagInfo" />
<xs:field xpath="BagId" />
</xs:unique>
<xs:unique name="BagChk_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
<xs:selector xpath=".//BagChk" />
<xs:field xpath="ChkId" />
</xs:unique>
<xs:keyref name="BAG_CHK_R" refer="BagInfo_Constraint1">
<xs:selector xpath=".//BagChk" />
<xs:field xpath="BagId" />
</xs:keyref>
<xs:keyref name="PSG_BAK_R" refer="Constraint1">
<xs:selector xpath=".//BagInfo" />
<xs:field xpath="PsgId" />
</xs:keyref>
</xs:element>
</xs:schema>
<PsgInfo>
<PsgName>李承民</PsgName>
<FltNo>MU501</FltNo>
<FltDate>2004-3-2</FltDate>
<SeatNo>10A</SeatNo>
<BrdNo>022</BrdNo>
<Contact>021-51136111</Contact>
<BrdPass>MU501 02 HKG022</BrdPass>
<CkiCounter>A03</CkiCounter>
<CkiTime>2004-03-02 15:12:38</CkiTime>
<PsgId>1</PsgId>
</PsgInfo>
<PsgInfo>
<PsgName>刘丽</PsgName>
<FltNo>MU501</FltNo>
<FltDate>2004-3-2</FltDate>
<SeatNo>10B</SeatNo>
<BrdNo>023</BrdNo>
<Contact>021-51136111</Contact>
<BrdPass>MU501 02 HKG023</BrdPass>
<CkiCounter>A03</CkiCounter>
<CkiTime>2004-03-02 15:15:38</CkiTime>
<PsgId>2</PsgId>
</PsgInfo>
<BagInfo>
<BagTag>0781123456</BagTag>
<CkiTime>2004-03-02 15:12:38</CkiTime>
<Channel>A03F</Channel>
<State>开包</State>
<PsgId>1</PsgId>
<BagId>1</BagId>
</BagInfo>
<BagChk>
<ChkId>1</ChkId>
<ForbidType>管制刀具</ForbidType>
<Amount>2</Amount>
<DealType>暂存</DealType>
<BagId>1</BagId>
</BagChk>
<BagChk>
<ChkId>2</ChkId>
<ForbidType>毒品</ForbidType>
<Amount>1</Amount>
<DealType>上报</DealType>
<BagId>1</BagId>
</BagChk>
</NewDataSet>
CreateFakeData();
SaveResultToXml(@"c:\tmp\bagLog.xml");
//将DataTables的数据先清掉,否则的确会造成约束上的问题:
dataSet1.Tables["BagChk"].Clear();
dataSet1.Tables["BagInfo"].Clear();
dataSet1.Tables["PsgInfo"].Clear();
LoadResultFromXml(@"c:\tmp\bagLog.xml");