本实例是一个从数据库里读出来省份,城市,地区的数据生成3个XML文件.
首先创建一个Linq to SQL的类, 然后将省份,城市,地区的表拖入类中:
本实例是一个从数据库里读出来省份,城市,地区的数据生成3个XML文件.
首先创建一个Linq to SQL的类, 然后将省份,城市,地区的表拖入类中:
![](https://www.cnblogs.com/images/cnblogs_com/wilsonwu/125994/e5fe07e91dd5d531b90e2db5.jpg)
然后新建一个类,加入代码如下:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public void ResetProvinces(string path)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
XElement xmlProvinces = new XElement("Provinces");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (S_Province province in DB.S_Provinces)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
xmlProvinces.Add(
new XElement("Province", province.ProvinceName.ToString(),
new XAttribute("ID", province.ProvinceID.ToString()),
new XAttribute("ProvinceName", province.ProvinceName.ToString())
)
);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
xmlProvinces.Save(@path);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public void ResetCities(string path)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
XElement xmlCities = new XElement("Cities");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (S_City city in DB.S_Cities)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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())
)
);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
xmlCities.Save(@path);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
public void ResetDistract(string path)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
XElement xmlDistricts =new XElement("Districts");
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
foreach (S_District district in DB.S_Districts)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
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())
)
);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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>
开博第一帖,虽然代码不算什么经典,也借此纪念,希望日后在这里结交更多朋友,谢谢!