Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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();
          
        }
	}
}
Posted
Comments
Sergey Alexandrovich Kryukov 27-Jan-11 21:55pm    
Where is your XML? What does it mean "it does not load"? Where is your exception information, in what line of code?
shakil0304003 28-Jan-11 0:02am    
Give your exception?

string strFile = "test.xml";

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


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.
 
Share this answer
 
What do you mean, it does not load the xml?

C#
string strFile = "test1.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?
 
Share this answer
 
Comments
Rei ho 27-Jan-11 21:57pm    
my XML is inside the folder, or do i need to hardcode pathing is all the way from C:/?
there is no exception info,


when i debugged it, my labels are blank, instead of showing the content from the XML
Rei ho 27-Jan-11 22:20pm    
it is a expression blend 4, and visual studio C# for event handling application.
i setted the XML to embedded resources, i get no exception, with no error.

if i give it the path, meaning
C:\...\xml
how do i code it, cause i tried string strFile = "c:/.../test1.xml";
and a red underline(error) came out

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900