生成
IAreaServiceAgent service = new AreaServiceAgent(EntityConnectionAgent.EntityAgent.GetConnection());
List<Province> list = new List<Province>();
ResultBase result = service.GetProvinces();
if (result.HasError)
{
App.ShowErrorMessage(result);
}
else
{
var items = result.Results.Cast<Province>().OrderBy(x=>x.Name).ToList();
items.ForEach(x => list.Add(x));
}
XmlDocument xmldoc = new XmlDocument();
XmlText xmltext;
//声明
XmlNode xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
xmlnode.InnerText += " encoding=\"UTF-8\"";
xmldoc.AppendChild(xmlnode);
//添加根节点
XmlElement xmlelementroot = xmldoc.CreateElement("","Body", "");
foreach (var item in list.OrderBy(x=>x.Name).ToList())
{
//添加一个元素
XmlElement elemProvince = xmldoc.CreateElement("",item.Name, "");
foreach (var city in item.Citys.OrderBy(x => x.Name).ToList())
{
XmlElement elemCity = xmldoc.CreateElement("",city.Name, "");
elemProvince.AppendChild(elemCity);
break;
}
xmlelementroot.AppendChild(elemProvince);
}
xmldoc.AppendChild(xmlelementroot);
//保存
xmldoc.Save("d:\\data1.xml");
MessageBox.Show("Ok");
//读取
'读取xml加载省份
Dim doc As New XmlDocument()
doc.Load(Application.StartupPath & "/Area.xml")
Dim nodeList As XmlNodeList = doc.SelectSingleNode("Body").ChildNodes
'foreach (XmlNode xn in nodeList)//遍历所有子节点
comb_province.Items.Clear()
For Each xn As XmlNode In nodeList
Dim provinceName As String = xn.Name
comb_province.Items.Add(provinceName)
For Each elemItem As XmlNode In xn.ChildNodes
Dim row As DataRow = areaDt.NewRow()
row("province") = provinceName
row("city") = elemItem.Name
areaDt.Rows.Add(row)
Next
Dim rowProvince As DataRow = areaDt.NewRow()
rowProvince("province") = provinceName
rowProvince("city") = provinceName
areaDt.Rows.Add(rowProvince)
Next
comb_province.SelectedIndex = 0