首页 新闻 会员 周边

WPF中加载XML一直都错

0
[已解决问题] 解决于 2013-01-22 17:18

我新建一个WPF application,然后加一个xml文件  contact.xml,然后在

后台代码写

        public MainWindow()         {             InitializeComponent();

            XmlDocument xml = new XmlDocument();

            xml.Load("contact.xml");         }

 

就是无法加载,包这个错:

'The invocation of the constructor on type 'TestwpfFunction.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.  可是xml文件里面就只有一个声明啊,啥都没有,不管在哪里加载xml,都不行

找不出来是哪的问题,求大神赐教!!!!!!

FallingStone的主页 FallingStone | 初学一级 | 园豆:38
提问于:2012-10-24 10:31
< >
分享
最佳答案
0
public string XmLload = string.Empty;
         XDocument _xDoc;
         public InitApp()
         {
             try
             {
                 XmLload = @Environment.CurrentDirectory + "\\Configs\\UserConfig.xml";
                 OpenUserFile(XmLload);
             }
             catch (TypeLoadException ex)
             {
                 MessageBox.Show(ex.Message);
             }
             catch (FileNotFoundException)
             {
                 throw new FileNotFoundException("系统配置文件未找到!");
             }
             catch (Exception ex)
             {
                 throw ex;
             }
}
  void OpenUserFile(string xmlUserFile)
         {
             try
             {
                 _xDoc = XDocument.Load(xmlUserFile);
                 if (_xDoc.Root != null)
                 {
                     var items1 = from c1 in _xDoc.Root.Elements("WCFUSER")
                                  let xElement = c1.Element("uname")
                                  where xElement != null && xElement.Value == "userconfig"
                                  select c1;
                 

                 }
         
             }
             catch (DirectoryNotFoundException ex)   //找不到当前的目录
             {
                 Common.ConfigLog.SetLog(ex, "读取用户配置文件:OpenUserFile");
             }
         }

 我的就是这样加载的。。没有报错啊。。

 
奖励园豆:5
在 水 一 方 | 小虾三级 |园豆:1097 | 2012-10-31 16:02
其他回答(2)
0

点击“contact.xml”文件,右键“属性”,“复制到输出目录”修改为“始终复制” ;”生成操作“修改为“嵌入的资源”。然后编译下项目。OK了

xuebingz | 园豆:143 (初学一级) | 2012-10-24 11:29

更改了,还是不行,还是一样的错误

支持(0) 反对(0) FallingStone | 园豆:38 (初学一级) | 2012-10-24 12:27
0

请问wpf中的按钮如何加载背景图片啊<Grid Height="507" Width="552">
        <Button HorizontalAlignment="Left" Margin="20,22,0,0" Name="button1" Width="128" Height="134" VerticalAlignment="Top">
            <Button.Content>
                <Image Source=".\Images\wpf.jpg" Stretch="Uniform"/>
            </Button.Content>
            </Button>
    </Grid> 这样不可以吗

未页 | 园豆:226 (菜鸟二级) | 2012-11-19 14:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册