首页 > 解决方案 > how to give xml file source in app.config and write/read those xml files form winform?

问题描述

DataSet ds = CreateDynamicDataSet();
ds.WriteXml(@"E:\AppScienti\AppScienti-POSLite\POSLite-Dev\XML\POSLite.xml");

private DataSet CreateDynamicDataSet()
{

    DataSet ds = new DataSet("DS");
    //ds.Namespace = "StdNamespace";
    dtOutletMapping.TableName = "Tables[2]";
    ds.Tables.Add(dtOutletMapping);
    dtxmlEmployee.TableName = "Tables[0]";
    ds.Tables.Add(dtxmlEmployee);
    dtxmlOrg.TableName = "Tables[1]";
    ds.Tables.Add(dtxmlOrg);

    return ds;
}

In above code I have path directly in winform and its working fine, now I want to keep path in app.config and use a path to write a dataset into file any possible solution please?

标签: asp.netwinformsapp-configcsharpcodeprovider

解决方案


确保您已添加对System.Configuration. (右键单击项目并选择“添加引用”并在“.Net”选项卡中找到它)。

修改 app.config 文件,如下所示: 在 下appSettings,添加您的文件名,如下所示。

<configuration>
  <appSettings>
    <add key="xmlFile" value="c:\\users\\test.xml" />
  </appSettings>
</configuration>

现在在代码中添加命名空间

using System.Configuration;

当你想读取文件名时,如下所示

string filename = ConfigurationManager.AppSettings.Get("xmlFile");

DataSet ds = CreateDynamicDataSet();
ds.WriteXml(filename);

推荐阅读