Dataset的WriteXml是按记录一条一条的写成XML,对应的,不可能给你写成分省市的,必须要自己写循环判断,自己构造xml
用select * from [TableName] order by PROVINCE, CITY, COUNTRY, TOWN这条语句查询出结果,这样记录是按省市乡镇依次排列的,读到DataTable里,再循环读取,当前记录与前一条记录进行比对,如果省市乡都一样就追加“<town name="" /> ”,如果省市一样,追加<country>,如果只是省一样,追加<city>,全部遍历一遍,构造xml字符串即可。
补充:
protected void WriteXML()
{
DataTable dt = new YourClass().YourMethod();//你的类.你的方法
string str = "<address>";
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i == 0)
{
str += "<province name=\"" + dt.Rows[i]["Province"].ToString() + "\">" +
"<city name=\"" + dt.Rows[i]["City"].ToString() + "\">" +
"<country name=\"" + dt.Rows[i]["Country"].ToString() + "\">" +
"<town name=\"" + dt.Rows[i]["Town"].ToString() + "\" />";
}
else
{
if (dt.Rows[i]["Province"].ToString() == dt.Rows[i - 1]["Province"].ToString() &&
dt.Rows[i]["City"].ToString() == dt.Rows[i - 1]["City"].ToString() &&
dt.Rows[i]["Country"].ToString() == dt.Rows[i - 1]["Country"].ToString())
{
str += "<town name=\"" + dt.Rows[i]["Town"].ToString() + "\" />";
}
else if (dt.Rows[i]["Province"].ToString() == dt.Rows[i - 1]["Province"].ToString() &&
dt.Rows[i]["City"].ToString() == dt.Rows[i - 1]["City"].ToString())
{
str += "</country>";
str += "<country name=\"" + dt.Rows[i]["Country"].ToString() + "\">" +
"<town name=\"" + dt.Rows[i]["Town"].ToString() + "\" />";
}
else if (dt.Rows[i]["Province"].ToString() == dt.Rows[i - 1]["Province"].ToString())
{
str += "</country>";
str += "</city>";
str += "<city name=\"" + dt.Rows[i]["City"].ToString() + "\">" + "<country name=\"" + dt.Rows[i]["Country"].ToString() + "\">" +
"<town name=\"" + dt.Rows[i]["Town"].ToString() + "\" />";
}
else
{
str += "</province>";
}
if (i == dt.Rows.Count - 1)
{
str += "</country>";
str += "</city>";
str += "</province>";
}
}
}
str += "</address>";
}
结果如图
Dataset的WriteXml是按记录一条一条的写成XML,对应的,不可能给你写成分省市的,必须要自己写循环判断,自己构造xml
用select * from [TableName] order by PROVINCE, CITY, COUNTRY, TOWN这条语句查询出结果,这样记录是按省市乡镇依次排列的,读到DataTable里,再循环读取,当前记录与前一条记录进行比对,如果省市乡都一样就追加“<town name="" /> ”,如果省市一样,追加<country>,如果只是省一样,追加<city>,全部遍历一遍,构造xml字符串即可。
补充:
protected void WriteXML()
{
DataTable dt = new YourClass().YourMethod();//你的类.你的方法
string str = "<address>";
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i == 0)
{
str += "<province name=\"" + dt.Rows[i]["Province"].ToString() + "\">" +
"<city name=\"" + dt.Rows[i]["City"].ToString() + "\">" +
"<country name=\"" + dt.Rows[i]["Country"].ToString() + "\">" +
"<town name=\"" + dt.Rows[i]["Town"].ToString() + "\" />";
}
else
{
if (dt.Rows[i]["Province"].ToString() == dt.Rows[i - 1]["Province"].ToString() &&
dt.Rows[i]["City"].ToString() == dt.Rows[i - 1]["City"].ToString() &&
dt.Rows[i]["Country"].ToString() == dt.Rows[i - 1]["Country"].ToString())
{
str += "<town name=\"" + dt.Rows[i]["Town"].ToString() + "\" />";
}
else if (dt.Rows[i]["Province"].ToString() == dt.Rows[i - 1]["Province"].ToString() &&
dt.Rows[i]["City"].ToString() == dt.Rows[i - 1]["City"].ToString())
{
str += "</country>";
str += "<country name=\"" + dt.Rows[i]["Country"].ToString() + "\">" +
"<town name=\"" + dt.Rows[i]["Town"].ToString() + "\" />";
}
else if (dt.Rows[i]["Province"].ToString() == dt.Rows[i - 1]["Province"].ToString())
{
str += "</country>";
str += "</city>";
str += "<city name=\"" + dt.Rows[i]["City"].ToString() + "\">" + "<country name=\"" + dt.Rows[i]["Country"].ToString() + "\">" +
"<town name=\"" + dt.Rows[i]["Town"].ToString() + "\" />";
}
else
{
str += "</province>";
}
if (i == dt.Rows.Count - 1)
{
str += "</country>";
str += "</city>";
str += "</province>";
}
}
}
str += "</address>";
}
结果如图
说明:只支持一个省的,要是多个省的你再稍做改动