zoukankan      html  css  js  c++  java
  • WCF和WPF读取xml的路径问题

    使用WCF编写服务,涉及到xml的读取,使用了System.AppDomain.CurrentDomain.BaseDirectory来获取路径,获得的是项目的基目录 例如: string path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @Data.xml); xml文件: ?xmlversion= 1.0 encoding= utf-8 ? Users User UserName Sky300 /Us
      

      使用WCF编写服务,涉及到xml的读取,使用了System.AppDomain.CurrentDomain.BaseDirectory来获取路径,获得的是项目的基目录

      例如:

      string path = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"Data.xml");

      xml文件:

    <?xml version="1.0" encoding="utf-8" ?>
    <Users>
    <User>
    <UserName>Sky300</UserName>
    <Password>123</Password>
    <Age>25</Age>
    </User>
    <User>
    <UserName>QYB</UserName>
    <Password>123</Password>
    <Age>25</Age>
    </User>
    <User>
    <UserName>ZJ</UserName>
    <Password>123</Password>
    <Age>25</Age>
    </User>
    </Users>

      WCF的服务的实现:

    public List<UserInfo> GetData()
    {
    List<UserInfo> list = new List<UserInfo>();
    string path = System.IO.Path.Combine(Environment.CurrentDirectory, @"Data.xml");
    string path1 = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, @"Data.xml");
    XDocument xmldoc=XDocument.Load(path1);
    var users = xmldoc.Descendants("User");
    foreach (var item in users)
    {
    UserInfo info = new UserInfo();
    info.UserName = item.Element("UserName").Value;
    info.Password = item.Element("Password").Value;
    info.Age =Convert.ToInt32(item.Element("Age").Value);
    list.Add(info);
    }
    return list;
    }

      在WPF中涉及到xml的读取,使用了Environment.CurrentDirectory

      例如:

      string path = System.IO.Path.Combine(Environment.CurrentDirectory, @"Data.xml");

      1. Application.StartupPath——获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

      2. Environment.CurrentDirectory——获取和设置当前目录(即该进程从中启动的目录)的完全限定路径,只是“当前”目录,不一定是exe文件所在的目录。在用FileOpenDialog选择一个文件以后这个目录就跟着发生改变了。

      3. Application.ExecutablePath——获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

  • 相关阅读:
    以JPanel为基础实现一个图像框
    扩展JButton实现自己的图片按钮
    箴言录2014年4月22日
    搜集整理一些Cron表达式例子
    长途旅行感悟
    箴言录2014年4月19日
    Linux下显示硬盘空间的两个命令
    Linux命令复习和练习_02
    Dash:程序员的的好帮手
    Linux的桌面环境gnome、kde、xfce、lxde 等等使用比较
  • 原文地址:https://www.cnblogs.com/aaa6818162/p/3620222.html
Copyright © 2011-2022 走看看