?
Solved

XML Document Loading

Posted on 2006-06-11
4
Medium Priority
?
236 Views
Last Modified: 2010-04-16
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MSXML2;

namespace ManipulateXML
{
    public partial class Form1 : Form
    {
        private DOMDocument40 doc;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string search = listBox1.SelectedItem.ToString();
            IXMLDOMNode nd = doc.selectSingleNode("bookstore/book[@ISBN=' " + search + " ' ]");
            MessageBox.Show(nd.text);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            doc = new DOMDocument40();
            doc.load("books.xml");
            IXMLDOMNodeList nodes;
            nodes = doc.selectNodes("bookstore/book");
            IXMLDOMNode node = nodes.nextNode();

            while (node != null)
            {
                listBox1.Items.Add(node.attributes.getNamedItem("ISBN").text);
                node = nodes.nextNode();
            }
        }
    }
}

this is a pretty simple code to understand. Button click loads an XML document called books.xml and I try to get information about the ISBN of all book nodes.
I compile and it compiles but on button click nothing happens. When i ran debugger I saw that 'while (node != null ' is a false statement. Please help
0
Comment
Question by:deersuper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 13

Expert Comment

by:dungla
ID: 16883089
Please paste your xml file here
0
 
LVL 13

Expert Comment

by:dungla
ID: 16883093
And you can use System.XML instead of MSXML2 if no special purpose.
0
 
LVL 2

Author Comment

by:deersuper
ID: 16883580
<?xml version="1.0" encoding="utf-8" ?>
<bookstore>
  <book genre="autobiography" publicationdate="1981" ISBN="1-816003-11-0">
    <title>title 1</title>
    <author>
      <first-name>ben </first-name>
      <last-name>franklin</last-name>
    </author>
    <price>899</price>
  </book>

  <book genre="novel" publicationdate="1967" ISBN="1-768503-11-0">
    <title>title 2</title>
    <author>
      <first-name>usman </first-name>
      <last-name>lakjai</last-name>
    </author>
    <price>299</price>
  </book>

  <book genre="sex" publicationdate="1381" ISBN="1-300618-11-0">
    <title>chudai</title>
    <author>
      <first-name>azam </first-name>
      <last-name>franklin</last-name>
    </author>
    <price>999</price>
  </book>
</bookstore>

the code book i am using specifically asks for using msxml2. thats why I used it !!!!!! thanks
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1000 total points
ID: 16883983
As mentioned earlier, you'd be better off simply including the System.Xml namespace rather than using the older MSXML2.

Then you can use something more like:

        private void button1_Click(object sender, EventArgs e)
        {
            doc = new XmlDocument();
            doc.Load("C:\\Test.xml");

            XmlNodeList nodes = doc.SelectNodes("bookstore/book");

            foreach (XmlNode node in nodes)
                listBox1.Items.Add(node.Attributes["ISBN"].InnerText);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string search = ((ListBox)sender).SelectedItem.ToString();
            XmlNode nd = doc.SelectSingleNode("bookstore/book[@ISBN='" + search + "']");

            MessageBox.Show(nd.InnerText);
        }
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month9 days, 9 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question