Take Node Values From XML C#

Naimishforu
Posted by Naimishforu under C# category on | Points: 40 | Views : 2315
Please find below source code with example,

XML File :

<PRODUCT PRICE="600" SIZE="3.15' x 2.65'"> 
<OPTIONS NAME="FINISH" SELECTED_DATA="GOLD" SELECTED_LABEL="GOLD">
<OPTION LABEL="GOLD" />
<OPTION LABEL="YELLOW" />
</OPTIONS>
<OPTIONS NAME="ATTACHMENT" SELECTED_DATA="" SELECTED_LABEL="HELLO">
<OPTION LABEL="HELLO" DATA="" />
<OPTION LABEL="GG" DATA="" />

</OPTIONS>
</PRODUCT>


C#.NET Code :

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.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Xml.XPath;

namespace WpfApplication2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}

private void button1_Click(object sender, RoutedEventArgs e)
{
XPathDocument Doc = new XPathDocument("C:\\My.xml");
XPathNavigator navigator = Doc.CreateNavigator();
XPathNodeIterator iterator = navigator.Select("/PRODUCT/OPTIONS/@SELECTED_LABEL");
while (iterator.MoveNext())
{
Console.WriteLine(iterator.Current.Name);
Console.WriteLine(iterator.Current.Value);
}
}
}
}


Thanks :)

Comments or Responses

Login to post response