Click here to Skip to main content
15,888,065 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i have xml file in this file some data through book ID wise. i want to show specific Book ID detail in C# (Console Base Program) but i face an error what should i do where is my mistake please explain and solve my problem i m pasitng my XML file code as well as C# code.

---------------------
XML CODE
---------------------
XML
<?xml version="1.0"?>
<catalog><![CDATA[C#'s operators include : < > &]]>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications
      with XML.</description>
   </book>
   <book id="bk102">
      <author>Ralls, Kim</author>
      <title>Midnight Rain</title>
      <genre>Fantasy</genre>
      <price>5.95</price>
      <publish_date>2000-12-16</publish_date>
      <description>A former architect battles corporate zombies,
      an evil sorceress, and her own childhood to become queen
      of the world.</description>
   </book>




------------------------------
C# CODE
------------------------------

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace XMLPractice
{
    class Program
    {
        static void Main(string[] args)
        {
 XmlDocument xmldocument = new XmlDocument();
            xmldocument.Load("D:\\inventory.xml");
            XmlElement objxmlelement = xmldocument.DocumentElement;
            XmlNode xmlnode = objxmlelement.SelectSingleNode("book [@id=bk101]");
            Console.WriteLine(xmlnode.InnerText);
            Console.ReadKey();
        }
    }
}
Posted

1 solution

Try with -
XmlNode xmlnode = objxmlelement.SelectSingleNode("book [@id='bk101']");
 
Share this answer
 

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