Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用xml c显示数据表#_C#_Xml_Xmlwriter - Fatal编程技术网

C# 用xml c显示数据表#

C# 用xml c显示数据表#,c#,xml,xmlwriter,C#,Xml,Xmlwriter,我正在尝试使用数据表中的数据创建一个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"> &

我正在尝试使用数据表中的数据创建一个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>

2010-07-25
无通道1
房客在度假
123456
2010-07-26
无通道2
租客外出工作
123456
2010-07-25
无通道1
住院租户
我设法用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);
}
foreach(数据行中的数据行)
{
xmlCostCode=行[“CostCode”]。ToString();
xmlReason=行[“原因”].ToString();
xmlcoment=row[“PropertyCode”].ToString();
xmlFilePath=xmlFolderPath+“test”+“.xml”;
xmlContent=“2017-07-18”+xmlReason+”+xmlcoment+”;
XmlDocument xdoc=新的XmlDocument();
LoadXml(xmlContent);
保存(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>

2010-07-25
无通道1
房客在度假
123456

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


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

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

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

 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);
xmlContent=”“;
foreach(数据行中的数据行)
{
xmlCostCode=行[“CostCode”]。ToString();
xmlReason=行[“原因”].ToString();
xmlcoment=row[“PropertyCode”].ToString();
xmlFilePath=xmlFolderPath+“test”+“.xml”;
xmlContent=xmlContent+“2017-07-18”+xmlReason+”“+xmlcoment+”;
}
xmlContent=xmlContent+“”;
XmlDocument xdoc=新的XmlDocument();
LoadXml(xmlContent);
保存(xmlFilePath);
这样,您就可以用每一行构建字符串,一旦完成,您就可以每次创建XML文档。

 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);
xmlContent=”“;
foreach(数据行中的数据行)
{
xmlCostCode=行[“CostCode”]。ToString();
xmlReason=行[“原因”].ToString();
xmlcoment=row[“PropertyCode”].ToString();
xmlFilePath=xmlFolderPath+“test”+“.xml”;
xmlContent=xmlContent+“2017-07-18”+xmlReason+”“+xmlcoment+”;
}
xmlContent=xmlContent+“”;
XmlDocument xdoc=新的XmlDocument();
LoadXml(xmlContent);
保存(xmlFilePath);
这样,您就可以用每一行构建字符串,完成后,您就可以使用XML linq创建XML文档。

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

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用系统数据;
命名空间控制台应用程序65
{
班级计划
{
静态void Main(字符串[]参数)
{
DataTable dt=新的DataTable();
添加(“成本代码”,类型(int));
添加(“日期”,类型(日期时间));
添加(“原因”,类型(字符串));
添加(“注释”,类型(字符串));
添加(“PropertyCode”,typeof(int));
Add(新对象[]{1245,DateTime.Parse(“2010-07-25”),“无访问权1”,“租户正在度假”,123456});
Add(新对象[]{null,DateTime.Parse(“2010-07-26”),“无访问权2”,“租户外出工作”,123456});
Add(新对象[]{1453,DateTime.Parse(“2010-07-25”),“无访问权1”,“医院租户”,null});
字符串头=“”+
"" +
"";
XDocument doc=XDocument.Parse(标题);
XElement作业列表=doc.subjects(“作业列表”).FirstOrDefault();
foreach(dt.AsEnumerable()中的数据行)
{
XElement作业=新XElement(“作业”,新对象[]{
新的XAttribute(“id”,row.Field(“CostCode”)==null?”:row.Field(“CostCode”).ToString(),
新XElement(“日期”,行.字段(“日期”).ToString(“yyyy-MM-dd”),
新元素(“原因”,行字段(“原因”),
新XElement(“注释”,行字段(“注释”),
新的XElement(“ExternalJobNumber”,row.Field(“PropertyCode”)==null?”:row.Field(“PropertyCode”).ToString()
});
作业列表。添加(作业);
}
}
}
}
使用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);
            }
        }
    }

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
使用系统数据;
命名空间控制台应用程序65
{
班级计划
{
静态void Main(字符串[]参数)
{
DataTable dt=新的DataTable();
添加(“成本代码”,类型(int));
添加(“日期”,类型(日期时间));
添加(“原因”,类型(字符串));
添加(“注释”,类型(字符串));
添加(“PropertyCode”,typeof(int));
Add(新对象[]{1245,DateTime.Parse(“2010-07-25”),“无访问权1”,“租户正在度假”,123456});
Add(新对象[]{null,DateTime.Parse(“2010-07-26”),“无访问权2”,“租户外出工作”,123456});
dt.行