我如何在c#中显示来自XML的显示特定数据 [英] How I can show display specific data from XML in c#

查看:73
本文介绍了我如何在c#中显示来自XML的显示特定数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个文件中有一些xml文件,一些数据通过书籍ID明智。我想在C#(控制台基础程序)中显示特定的书ID详细信息,但我面临一个错误我该怎么办我的错误请解释并解决我的问题我使用我的XML文件代码以及C#代码。



---------------------

XML CODE
---------------------

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 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# CODE
------------------------------

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();
        }
    }
}

推荐答案

尝试 -

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


这篇关于我如何在c#中显示来自XML的显示特定数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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