以 xml c# 显示数据表



我正在尝试使用数据表中的数据创建一个xml文件。最终,这就是我想要创造的

<?xml version="1.0" encoding="utf-8"?>
<regisApts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<JobList>
<Job id="1245">
<Date>2010-07-25</Date>
<Reason>No Access 1</Reason>
<Comment>Tenant is on holiday</Comment>
<ExternalJobNumber>123456</ExternalJobNumber>
</Job>
<Job id="">
<Date>2010-07-26</Date>
<Reason>No Access 2</Reason>
<Comment>Tenant out at work</Comment>
<ExternalJobNumber>123456</ExternalJobNumber>
</Job>
<Job id="1453">
<Date>2010-07-25</Date>
<Reason>No Access 1</Reason>
<Comment>Tenant in hospital</Comment>
</Job>
</JobList>
</regisApts>

我设法用foreach生成了一个xml,但是在插入下一行时它会覆盖xml。任何帮助真的非常感谢。谢谢

foreach (DataRow row in dt.Rows)
{
xmlCostCode = row["CostCode"].ToString();
xmlReason = row["Reason"].ToString();
xmlComment = row["PropertyCode"].ToString();
xmlFilePath = xmlFolderPath + "test" + ".xml";
xmlContent = "<JobList><Job Id="" + xmlCostCode + ""><Date>2017-07-18</Date><Reason>" + xmlReason + "</Reason><Comment>" + xmlComment + "</Comment></Job></JobList>";
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xmlContent);
xdoc.Save(xmlFilePath);
}

我得到的结果

<JobList>
<Job id="1245">
<Date>2010-07-25</Date>
<Reason>No Access 1</Reason>
<Comment>Tenant is on holiday</Comment>
<ExternalJobNumber>123456</ExternalJobNumber>
</Job>
<JobList>

每次你这样做

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xmlContent);
xdoc.Save(xmlFilePath);

它用最近的一行覆盖您以前的文档,因此请尝试

xmlContent = "<regisApts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><JobList>";
foreach (DataRow row in dt.Rows)
{
xmlCostCode = row["CostCode"].ToString();
xmlReason = row["Reason"].ToString();
xmlComment = row["PropertyCode"].ToString();
xmlFilePath = xmlFolderPath + "test" + ".xml";
xmlContent = xmlContent +"<Job Id="" + xmlCostCode + ""><Date>2017-07-18</Date><Reason>" + xmlReason + "</Reason><Comment>" + xmlComment + "</Comment></Job>";
}
xmlContent = xmlContent +"</JobList></regisApts>";
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xmlContent);
xdoc.Save(xmlFilePath);

这样,您就可以用每一行构建字符串,一旦完成,您就可以创建 XML 文档。

当你说你正在使用for...each,我假设您正在以文本方式构建XML。我建议您不要这样做,而是研究 XML 序列化。Microsoft在这里提供了一个示例:

https://support.microsoft.com/en-us/help/815813/how-to-serialize-an-object-to-xml-by-using-visual-c

本质上,你为XML创建一个对象表示形式(你的数据表可能已经是这个了(,为你的类型创建一个序列化程序,然后运行它,输出到一个流(文件流等(

Using xml linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Data;
namespace ConsoleApplication65
{
class Program
{
static void Main(string[] args)
{
DataTable dt = new DataTable();
dt.Columns.Add("CostCode", typeof(int));
dt.Columns.Add("Date", typeof(DateTime));
dt.Columns.Add("Reason", typeof(string));
dt.Columns.Add("Comment", typeof(string));
dt.Columns.Add("PropertyCode", typeof(int));
dt.Rows.Add(new object[] { 1245, DateTime.Parse("2010-07-25"), "No Access 1", "Tenant is on holiday", 123456 });
dt.Rows.Add(new object[] { null, DateTime.Parse("2010-07-26"), "No Access 2", "Tenant out at work", 123456 });
dt.Rows.Add(new object[] { 1453, DateTime.Parse("2010-07-25"), "No Access 1", "Tenant in hospital", null });
string header = "<?xml version="1.0" encoding="utf-8"?>" +
"<regisApts xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">" +
"<JobList></JobList></regisApts>";
XDocument doc = XDocument.Parse(header);
XElement jobList = doc.Descendants("JobList").FirstOrDefault();
foreach(DataRow row in dt.AsEnumerable())
{
XElement job = new XElement("Job", new object[] {
new XAttribute("id", row.Field<object>("CostCode") == null ? "" : row.Field<int>("CostCode").ToString()),
new XElement("Date", row.Field<DateTime>("Date").ToString("yyyy-MM-dd")),
new XElement("Reason", row.Field<string>("Reason")),
new XElement("Comment", row.Field<string>("Comment")),
new XElement("ExternalJobNumber", row.Field<object>("PropertyCode") == null ? "" : row.Field<int>("PropertyCode").ToString())
});
jobList.Add(job);
}
}
}
}

除非由于某种原因必须使用 DataTable(我在这里没有看到一个好的(,否则您应该使用泛型将数据作为强类型对象返回。类似的东西

List<JobList> jobList = new List<JobList>();

然后使用函数将 JobList 类序列化为 XML。只需简单地将类作为对象传递给函数即可。我不喜欢使用 XMLSerializer 类,因为它很臃肿,并且在序列化对象时必须创建一个临时文件。我会使用这样的东西,它的速度要快得多。

public XElement GetAsXml(object obj)
{
XElement xelement = new XElement(obj.GetType().Name); 
PropertyInfo[] props = obj.GetType().GetProperties();
foreach (PropertyInfo prop in props)
{
object propertyValue = prop.GetValue(obj);
if(propertyValue != null)
{
XElement xProperty = new XElement(prop.Name, propertyValue);
xelement.Add(xProperty);
}
}
return xelement;
}

相关内容

  • 没有找到相关文章

最新更新