在xml c#中显示数据表

Display the datatable in 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>

当您说您正在使用 for...each 时,我假设您正在构建 XML 文本。我建议您 不要 那样做,而是研究 XML 序列化。微软在这里提供了一个例子:

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

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

每次你做

 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 文档。

使用 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);
            }
        }
    }

}

除非您出于某种原因必须使用数据表(我在这里没有看到好的数据表),否则您应该使用泛型 return 您的数据作为强类型对象。像

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

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

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;
    }