创建xml
需求:创建一个xml文档,其格式为:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<?xml version="1.0" encoding="gb2312" ?>
<chart caption='个人一年内月度得分统计' xAxisName='月份' yAxisName='分值' numberPrefix=''>
<set label='1月' value='14400' />
<set label='2月' value='19600' />
<set label='3月' value='24000' />
<set label='4月' value='15700' />
<set label='5月' value='14400' />
<set label='6月' value='19600' />
<set label='7月' value='24000' />
<set label='8月' value='15700' />
<set label='9月' value='14400' />
<set label='10月' value='19600' />
<set label='11月' value='24000' />
<set label='12月' value='15700' />
</chart>
代码:
private string CreatXml(DataTable dt)
{
string ss = "";
try
{
XDocument xdoc = new XDocument(new XDeclaration("1.0", "gb2312", "yes"));
XElement root = new XElement("chart",
new XAttribute("caption", "个人一年内月度得分统计"),
new XAttribute("xAxisName", "月份"),
new XAttribute("yAxisName", "分值"),
new XAttribute("numberPrefix", "")
);
for (int i = 0; i < dt.Rows.Count; i++)
{
XElement node = new XElement("set",
new XAttribute("label", dt.Rows[i]["label"]),
new XAttribute("value", dt.Rows[i]["value"])
);
root.Add(node);
}
xdoc.Add(root);
ss = xdoc.ToString();
}
catch (Exception)
{
throw;
}
return ss;
}