本实例是一个从数据库里读出来省份,城市,地区的数据生成3个XML文件.
首先创建一个Linq to SQL的类, 然后将省份,城市,地区的表拖入类中:
然后新建一个类,加入代码如下:
public void ResetProvinces(string path)
{
XElement xmlProvinces = new XElement("Provinces");
foreach (S_Province province in DB.S_Provinces)
{
xmlProvinces.Add(
new XElement("Province", province.ProvinceName.ToString(),
new XAttribute("ID", province.ProvinceID.ToString()),
new XAttribute("ProvinceName", province.ProvinceName.ToString())
)
);
}
xmlProvinces.Save(@path);
}
public void ResetCities(string path)
{
XElement xmlCities = new XElement("Cities");
foreach (S_City city in DB.S_Cities)
{
xmlCities.Add(
new XElement("City", city.CityName.ToString(),
new XAttribute("ID", city.CityID.ToString()),
new XAttribute("CityName", city.CityName.ToString()),
new XAttribute("PID", city.ProvinceID.ToString()),
new XAttribute("ZipCode", city.ZipCode.ToString())
)
);
}
xmlCities.Save(@path);
}
public void ResetDistract(string path)
{
XElement xmlDistricts =new XElement("Districts");
foreach (S_District district in DB.S_Districts)
{
xmlDistricts.Add(
new XElement("District", district.DistrictName.ToString(),
new XAttribute("ID", district.DistrictID.ToString()),
new XAttribute("DistrictName", district.DistrictName.ToString()),
new XAttribute("CID", district.CityID.ToString())
)
);
}
xmlDistricts.Save(@path);
}
最后执行过代码后可以看到XML效果:
(以城市为例)
<?xml version="1.0" encoding="utf-8"?>
<Cities>
<City ID="1" PID="1" ZipCode="100000">北京市</City>
<City ID="2" PID="2" ZipCode="100000">天津市</City>
<City ID="3" PID="3" ZipCode="050000">石家庄市</City>
<City ID="4" PID="3" ZipCode="063000">唐山市</City>
<City ID="5" PID="3" ZipCode="066000">秦皇岛市</City>
<City ID="6" PID="3" ZipCode="056000">邯郸市</City>
<City ID="7" PID="3" ZipCode="054000">邢台市</City>
<City ID="8" PID="3" ZipCode="071000">保定市</City>
<City ID="9" PID="3" ZipCode="075000">张家口市</City>
<City ID="10" PID="3" ZipCode="067000">承德市</City>
<City ID="11" PID="3" ZipCode="061000">沧州市</City>
<City ID="12" PID="3" ZipCode="065000">廊坊市</City>
<City ID="13" PID="3" ZipCode="053000">衡水市</City>
<City ID="14" PID="4" ZipCode="030000">太原市</City>
<City ID="15" PID="4" ZipCode="037000">大同市</City>
<City ID="16" PID="4" ZipCode="045000">阳泉市</City>
<City ID="17" PID="4" ZipCode="046000">长治市</City>
<City ID="18" PID="4" ZipCode="048000">晋城市</City>
<City ID="19" PID="4" ZipCode="036000">朔州市</City>
<City ID="20" PID="4" ZipCode="030600">晋中市</City>
<City ID="21" PID="4" ZipCode="044000">运城市</City>
<City ID="22" PID="4" ZipCode="034000">忻州市</City>
<City ID="23" PID="4" ZipCode="041000">临汾市</City>
<City ID="24" PID="4" ZipCode="030500">吕梁市</City>
<City ID="25" PID="5" ZipCode="010000">呼和浩特市</City>
<City ID="26" PID="5" ZipCode="014000">包头市</City>
<City ID="27" PID="5" ZipCode="016000">乌海市</City>
</Cities>
<Cities>
<City ID="1" PID="1" ZipCode="100000">北京市</City>
<City ID="2" PID="2" ZipCode="100000">天津市</City>
<City ID="3" PID="3" ZipCode="050000">石家庄市</City>
<City ID="4" PID="3" ZipCode="063000">唐山市</City>
<City ID="5" PID="3" ZipCode="066000">秦皇岛市</City>
<City ID="6" PID="3" ZipCode="056000">邯郸市</City>
<City ID="7" PID="3" ZipCode="054000">邢台市</City>
<City ID="8" PID="3" ZipCode="071000">保定市</City>
<City ID="9" PID="3" ZipCode="075000">张家口市</City>
<City ID="10" PID="3" ZipCode="067000">承德市</City>
<City ID="11" PID="3" ZipCode="061000">沧州市</City>
<City ID="12" PID="3" ZipCode="065000">廊坊市</City>
<City ID="13" PID="3" ZipCode="053000">衡水市</City>
<City ID="14" PID="4" ZipCode="030000">太原市</City>
<City ID="15" PID="4" ZipCode="037000">大同市</City>
<City ID="16" PID="4" ZipCode="045000">阳泉市</City>
<City ID="17" PID="4" ZipCode="046000">长治市</City>
<City ID="18" PID="4" ZipCode="048000">晋城市</City>
<City ID="19" PID="4" ZipCode="036000">朔州市</City>
<City ID="20" PID="4" ZipCode="030600">晋中市</City>
<City ID="21" PID="4" ZipCode="044000">运城市</City>
<City ID="22" PID="4" ZipCode="034000">忻州市</City>
<City ID="23" PID="4" ZipCode="041000">临汾市</City>
<City ID="24" PID="4" ZipCode="030500">吕梁市</City>
<City ID="25" PID="5" ZipCode="010000">呼和浩特市</City>
<City ID="26" PID="5" ZipCode="014000">包头市</City>
<City ID="27" PID="5" ZipCode="016000">乌海市</City>
</Cities>
开博第一帖,虽然代码不算什么经典,也借此纪念,希望日后在这里结交更多朋友,谢谢!