日期:2014-05-18  浏览次数:20902 次

c# 导出xml 请问各位高手
在.net,c#下我要把数据导出和导入到xml
请教各位高手怎么做,最好给个实例。


------解决方案--------------------
楼主试试下面的方法,最近看了看,应该对你有用的
string xmlFilePath = @"D:\Data\Area.xml";
try
{

XmlWriter writer=XmlWriter.Create(xmlFilePath);


writer.WriteStartDocument(false);

writer.WriteComment("This XML file represents the details of " +

"an Area");

writer.WriteStartElement("Country");

DataSet dtPro=null;
DataSet dtCity = null;
DataSet dtArea =null;

dtPro = AjaxMethod.GetPovinceList();
int procount=0,citycount=0,areacount=0;

for(int i=0;i<dtPro.Tables[0].Rows.Count;i++)
{
writer.WriteStartElement("province");
writer.WriteAttributeString("proid",dtPro.Tables[0].Rows[i]["id"].ToString());
writer.WriteElementString("proname", dtPro.Tables[0].Rows[i]["province"].ToString());
writer.WriteElementString("proId", dtPro.Tables[0].Rows[i]["provinceID"].ToString());

dtCity = AjaxMethod.GetCityList(Convert.ToInt32(dtPro.Tables[0].Rows[i]["provinceID"]));
for(int j=0;j<dtCity.Tables[0].Rows.Count;j++)
{

writer.WriteStartElement("city");
writer.WriteAttributeString("cityid",dtCity.Tables[0].Rows[j]["id"].ToString());

writer.WriteElementString("cityName", dtCity.Tables[0].Rows[j]["city"].ToString());

writer.WriteElementString("cityId", dtCity.Tables[0].Rows[j]["cityID"].ToString());



dtArea = AjaxMethod.GetAreaList(Convert.ToInt32(dtCity.Tables[0].Rows[j]["cityID"]));
for(int a=0;a<dtArea.Tables[0].Rows.Count;a++)
{
writer.WriteStartElement("area"); writer.WriteAttributeString("areaid",dtArea.Tables[0].Rows[a]["id"].ToString()); writer.WriteElementString("cityName", dtArea.Tables[0].Rows[a]["area"].ToString()); writer.WriteElementString("cityId", dtArea.Tables[0].Rows[a]["areaID"].ToString());
writer.WriteEndElement();
areacount++;
}
writer.WriteEndElement();
citycount++;
}

writer.WriteEndElement();

writer.Flush();
procount++;
this.Label1.Text = "File is written successfully"+"</br>"; this.Label1.Text += "省份:"+ procount.ToString()+"条, 城市:"+citycount.ToString()+"条,区域:"+ areacount.ToString()+" 条";

}


}

catch (Exception ex)

{

this.Label1.Text = "An Exception occurred: " + ex.Message;

}

------解决方案--------------------
http://www.cnblogs.com/bullfrog/archive/2006/01/29/324147.aspx
讲的蛮详细的,如果直接是DataSet转换XML那就直接用DataSet的WriteXml和ReamXml
------解决方案--------------------
//导入的
XmlDocument document = new XmlDocument();
document.Load(dygl2005.WholeObj.AppPath + "\\XMLSETCOLUMN.xml");

XmlNodeList objXList = document.SelectNodes("//setcolumn//" + formname + "ds");

string lm = string.Empty;
//遍历符合条件节点
foreach (XmlNode objNode in objXList)
{
XmlNodeReader objNdRd = new XmlNodeReader(objNode);

while (objNdRd.Read())
{
if (objNdRd.NodeType ==