加载并显示XML [英] Loading and display XML

查看:57
本文介绍了加载并显示XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,
我正在尝试使用expresison blend和VS2010从XML获取标签内容,以下是我在VS2010中的编码,当我按下按钮时,它不会加载XML内容,我是否必须将xml绑定到表达式混合处的标签?我该怎么办?

Hello,
i am trying to get content for labels from a XML, using expresison blend and VS2010, below is my coding in VS2010, when i press the buttons, it doesn''t load the XML content, do i have to bind the xml to the labels at expression blend? how do i do that?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Xml;
using System.IO;
using System.Net;

namespace testing123
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{ XmlDocument doc;

		public MainWindow()
		{
			InitializeComponent();
            LoadXML();  
            GetTextForControl();
            LoadXML2();


			// Insert code required on object creation below this point.
		}
        private void GetTextForControl()
        {
            foreach (UIElement element in spTest.Children)
            {
                if (element is Button)
                {
                    ((Button)element).Content = FindTextForControl(((Button)element).Name);
                }
                if (element is TextBlock)
                {
                    ((TextBlock)element).Text = FindTextForControl(((TextBlock)element).Name);
                }
            }
        }
private string FindTextForControl(string sName)
 {
  string strText = string.Empty;

  XmlNode node = doc.DocumentElement.SelectSingleNode(string.Format("Control[@Name=''{0}'']", sName));
  if (node != null)
  {
  strText = node.Attributes["Content"].Value;
  }

  return strText;
 }

 private void LoadXML()
 {
  string strFile = "test.xml";

  try
  {
  doc = new XmlDocument();
  doc.Load(strFile);
  
  
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }
 }
 private void LoadXML2()
 {

     string strFile = "test1.xml";

     try
     {
         doc = new XmlDocument();
         doc.Load(strFile);


     }
     catch (Exception ex)
     {
         MessageBox.Show(ex.Message);
     }
 }


        private void Page1_Click(object sender, RoutedEventArgs e)
 {
     LoadXML();
            page1 newapp = new page1();
            newapp.Show();
            this.Close();   
           



        }

        private void Page2_Click(object sender, RoutedEventArgs e)
        {
            LoadXML2();
            page1 newapp = new page1();
            newapp.Show();
            this.Close();
          
        }
	}
}

推荐答案

string strFile = "test.xml";

  try
  {
  doc = new XmlDocument();
  doc.Load(strFile);
  }
  catch (Exception ex)
  {
  MessageBox.Show(ex.Message);
  }



如果将xml文件路径写为"test.xml",则必须将xml文件放在程序的exe文件目录中.



If you write xml file path as "test.xml" then you have to put the xml file in the directory of the program''s exe file.


这是什么意思,它确实无法加载xml?

What do you mean, it does not load the xml?

string strFile = "test1.xml";



xml是否在应用程序所在的根目录中?是目录搜索路径吗?您需要给它路径吗?

另外,您是否遇到异常,您在消息框中得到了什么?



Is the xml in your root directory where your app is located? Is it the directory search path? DO you need to give it the path?

Also, do you get an exception, what did you get in your message box?


这篇关于加载并显示XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆