C#用文件流写入XML文件出错
using(SqlConnectioncon=newSqlConnection("DataSource=.;InitialCatalog=HotelManage;Inte...
using (SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=HotelManage;Integrated Security=True"))
{
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select * from roomType",con);
DataSet set = new DataSet();
adapter.Fill(set);
con.Close();
//创建文件流对象
FileStream str = new FileStream(@"E:\computer\s2\C# 资料\NET三层结构应用开发(李翔)\NetThreeLayouApplication\XML文件的操作\roomType.xml",FileMode.Create,FileAccess.Write);
//创建文件操作对象
StreamWriter writer = new StreamWriter(str);
//读取数据库中的信息
str.SetLength (0);
writer.WriteLine("<?xml version='1.0' encoding='utf-8' ?> ");
writer.WriteLine("<roomtype>");
foreach(DataRow row in set.Tables[0].Rows)
{
writer.WriteLine("<TypeID >" + row[0] + " </TypeID> ");
writer.WriteLine("<TypeName>" + row[1] + " </TypeName> ");
writer.WriteLine("<TypePrice>" + row[2] + " </TypePrice> ");
writer.WriteLine("<AddBedPrice>" + row[3] + " </AddBedPrice> ");
writer.WriteLine("<IsAddBed>" + row[4] + " </IsAddBed> ");
writer.WriteLine("<Remark>" + row[5] + " </Remark> ");
}
writer.WriteLine("</roomtype>");
str.Close();
MessageBox.Show("写入文件成功");
结果确为;
<?xml version='1.0' encoding='utf-8' ?>
<roomtype>
<TypeID >1 </TypeID>
<TypeName>标间 </TypeName>
<TypePrice>160.0000 </TypePrice>
<AddBedPrice>600.0000 </AddBedPrice>
<IsAddBed>是 </IsAddBed>
<Remark>没有独立卫生间!! </Remark>
<TypeID >5 </TypeID>
<TypeName>套间 </TypeName>
<TypePrice>800.0000 </TypePrice>
<AddBedPrice>80.0000 </AddBedPrice>
<IsAddBed>是 </IsAddBed>
<Remark>包括两个卧室 </Remark>
<TypeID >6 </TypeID>
<TypeName>豪华间 </TypeName>
<TypePrice>2600.0000 </TypePrice>
<AddBedPrice>160.0000 </AddBedPrice>
<IsAddBed>是 </IsAddBed>
<Remark> </Remark>
<TypeID >8 </TypeID>
<TypeName>贵宾间 </TypeName>
<TypePrice>600.0000 </TypePrice>
<AddBedPrice>0.0000 </AddBedPrice>
<IsAddBed>否 </IsAddBed>
<Remark> </Remark>
<TypeID >10 </TypeID>
<TypeName>总统套房 </TypeName>
<TypePrice>2600.0000 </TypePrice>
<AddBedPrice>0.0000 </AddBedPrice>
<IsAddBed>否 </IsAddBed>
<Remark> </Remark>
<TypeID >11 </TypeID>
<TypeName>至尊套房 </TypeName>
没有全部读取出来,怎么回事 展开
{
con.Open();
SqlDataAdapter adapter = new SqlDataAdapter("select * from roomType",con);
DataSet set = new DataSet();
adapter.Fill(set);
con.Close();
//创建文件流对象
FileStream str = new FileStream(@"E:\computer\s2\C# 资料\NET三层结构应用开发(李翔)\NetThreeLayouApplication\XML文件的操作\roomType.xml",FileMode.Create,FileAccess.Write);
//创建文件操作对象
StreamWriter writer = new StreamWriter(str);
//读取数据库中的信息
str.SetLength (0);
writer.WriteLine("<?xml version='1.0' encoding='utf-8' ?> ");
writer.WriteLine("<roomtype>");
foreach(DataRow row in set.Tables[0].Rows)
{
writer.WriteLine("<TypeID >" + row[0] + " </TypeID> ");
writer.WriteLine("<TypeName>" + row[1] + " </TypeName> ");
writer.WriteLine("<TypePrice>" + row[2] + " </TypePrice> ");
writer.WriteLine("<AddBedPrice>" + row[3] + " </AddBedPrice> ");
writer.WriteLine("<IsAddBed>" + row[4] + " </IsAddBed> ");
writer.WriteLine("<Remark>" + row[5] + " </Remark> ");
}
writer.WriteLine("</roomtype>");
str.Close();
MessageBox.Show("写入文件成功");
结果确为;
<?xml version='1.0' encoding='utf-8' ?>
<roomtype>
<TypeID >1 </TypeID>
<TypeName>标间 </TypeName>
<TypePrice>160.0000 </TypePrice>
<AddBedPrice>600.0000 </AddBedPrice>
<IsAddBed>是 </IsAddBed>
<Remark>没有独立卫生间!! </Remark>
<TypeID >5 </TypeID>
<TypeName>套间 </TypeName>
<TypePrice>800.0000 </TypePrice>
<AddBedPrice>80.0000 </AddBedPrice>
<IsAddBed>是 </IsAddBed>
<Remark>包括两个卧室 </Remark>
<TypeID >6 </TypeID>
<TypeName>豪华间 </TypeName>
<TypePrice>2600.0000 </TypePrice>
<AddBedPrice>160.0000 </AddBedPrice>
<IsAddBed>是 </IsAddBed>
<Remark> </Remark>
<TypeID >8 </TypeID>
<TypeName>贵宾间 </TypeName>
<TypePrice>600.0000 </TypePrice>
<AddBedPrice>0.0000 </AddBedPrice>
<IsAddBed>否 </IsAddBed>
<Remark> </Remark>
<TypeID >10 </TypeID>
<TypeName>总统套房 </TypeName>
<TypePrice>2600.0000 </TypePrice>
<AddBedPrice>0.0000 </AddBedPrice>
<IsAddBed>否 </IsAddBed>
<Remark> </Remark>
<TypeID >11 </TypeID>
<TypeName>至尊套房 </TypeName>
没有全部读取出来,怎么回事 展开
4个回答
展开全部
using(StreamWriter writer = new StreamWriter(str))
{
//读取数据库中的信息
str.SetLength (0);
writer.WriteLine("<?xml version='1.0' encoding='utf-8' ?> ");
writer.WriteLine("<roomtype>");
foreach(DataRow row in set.Tables[0].Rows)
{
writer.WriteLine("<TypeID >" + row[0] + " </TypeID> ");
writer.WriteLine("<TypeName>" + row[1] + " </TypeName> ");
writer.WriteLine("<TypePrice>" + row[2] + " </TypePrice> ");
writer.WriteLine("<AddBedPrice>" + row[3] + " </AddBedPrice> ");
writer.WriteLine("<IsAddBed>" + row[4] + " </IsAddBed> ");
writer.WriteLine("<Remark>" + row[5] + " </Remark> ");
}
writer.WriteLine("</roomtype>");
str.Close();
}
像我上面这么弄,应该就好了
把这段代码用using框起来,即using{代码.....}
{
//读取数据库中的信息
str.SetLength (0);
writer.WriteLine("<?xml version='1.0' encoding='utf-8' ?> ");
writer.WriteLine("<roomtype>");
foreach(DataRow row in set.Tables[0].Rows)
{
writer.WriteLine("<TypeID >" + row[0] + " </TypeID> ");
writer.WriteLine("<TypeName>" + row[1] + " </TypeName> ");
writer.WriteLine("<TypePrice>" + row[2] + " </TypePrice> ");
writer.WriteLine("<AddBedPrice>" + row[3] + " </AddBedPrice> ");
writer.WriteLine("<IsAddBed>" + row[4] + " </IsAddBed> ");
writer.WriteLine("<Remark>" + row[5] + " </Remark> ");
}
writer.WriteLine("</roomtype>");
str.Close();
}
像我上面这么弄,应该就好了
把这段代码用using框起来,即using{代码.....}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
DataSet.wretieXML()多好
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
DataSet提供了序列化和反序列化的方法。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询