【问题标题】:how to give xml file source in app.config and write/read those xml files form winform?如何在 app.config 中提供 xml 文件源并从 winform 中写入/读取这些 xml 文件?
【发布时间】:2018-10-25 05:31:41
【问题描述】:
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;
}

在上面的代码中,我直接在 winform 中有路径并且它工作正常,现在我想将路径保留在 app.config 中并使用路径将数据集写入文件 请问有什么可能的解决办法吗?

【问题讨论】:

    标签: asp.net winforms app-config csharpcodeprovider


    【解决方案1】:

    确保您已添加对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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多