【问题标题】:Create write and save xml file dynamically in c# .net在 c# .net 中动态创建写入和保存 xml 文件
【发布时间】:2016-12-20 18:03:19
【问题描述】:

我想创建 xml 文件并从数据库中写入数据。文件将动态创建。

我将数据存储在 DataTable 中。查询是select documentId,documentContent from tblDocument where status = 'F'

其中 documentContent 是 xml 数据。

我尝试了以下代码,但它不起作用,

foreach(DataRow dr in dt.Rows)
{
    string filepath =  ConfigurationManager.Appsetings[Constants.FailedDocuments];
    string filename = "message_"+ dr["documentId"].ToString();
    string content = dr["documentContent"].ToString();
    XDocument xdoc = new XDocument();
    xdoc.parse(content);
    xdoc.Load(filepath+filename);
}

我是新手,不知道如何以及在哪里正确放置此代码,因为我想写content

【问题讨论】:

  • 你说的“不工作”是什么意思
  • 您的意思是xdoc.Save 而不是Load

标签: c# .net xml linq-to-xml writexml


【解决方案1】:

两件事:

  1. 请发布正确的代码。 XDocument 类没有实例方法 “解析”,只有“解析”。 XDocument 类没有实例方法 “加载”,只有静态方法“加载”。
  2. xdoc.Parse(content) 将创建 来自字符串的 XDocument。 XDocument.Load(filename) 将返回一个 XDocument 从 XML 文件“文件名”加载。

这样就可以了:

foreach(DataRow dr in dt.Rows) {
   string filepath =  ConfigurationManager.Appsetings[Constants.FailedDocuments];
   string filename = "message_"+ dr["documentId"].ToString();
   string content = dr["documentContent"].ToString();
   XDocument xdoc = new XDocument();
   xdoc.Parse(content);
   xdoc.Save(filepath+filename);
}

【讨论】:

  • 是的,这行得通。只需要很小的改变。 XDocument xdoc = Xdocument.Parse(content);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-29
  • 1970-01-01
相关资源
最近更新 更多