如何在C#2.0 / 3.0上选择指定值的xmlelements xmlnodes xml属性 [英] how to select xmlelements xmlnodes xmlattributes of specified values on c# 2.0/3.0

查看:46
本文介绍了如何在C#2.0 / 3.0上选择指定值的xmlelements xmlnodes xml属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的xml文档,看起来像这样

here is my xml document that looks like this

<?xml version="1.0" encoding="UTF-8"?>
<bookstore> 
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year updated="no" version="3.5">2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="fr">Harry Potter</title>
<author>J K. Rowling</author>
<year updated="yes" version="2.0">2005</year>
<price>29.99</price>
</book>
</bookstore>

1-如何选择所有图书和打印其元素的文本(标题,作者,...)?

1-how to select all books and print their element's text (title,author,...) ?

2-如何仅获取值 lang 来自 标题版本从每本书的开始?

2-how to get only the value of lang from title and version from year for each book ?

3-如何选择所有 books where category = cooking 并打印所有元素?

3-how to select all books where category="cooking" and print all the elements ?

4-如何选择带有标题的 lang 的所有书籍 en 并打印其所有元素值吗?

4-how to select all books where the lang of titleis en and print all their elements values ?

推荐答案

在这种情况下,这些框架版本不支持 XDocument class ,因此我们使用 XmlDocument

in this case theses versions of framework does not support XDocument class so we use XmlDocument

 XmlDocument myXmlDoc = new XmlDocument();
 myXmlDoc.Load(@"c:\books.xml"); //wich is above you can use uri location
 XmlNodeList list = myXmlDoc.SelectNodes("/bookstore/book");
  //Selecting all book nodes then extract title author year  
  foreach (XmlNode mynode in list)
  {
  Console.WriteLine(mynode["title"].InnerText);
  Console.WriteLine(mynode["author"].InnerText);
  Console.WriteLine(mynode["year"].InnerText);
  Console.WriteLine(mynode["price"].InnerText);
  Console.WriteLine("------------");
  }
  //OUTPUT :
  Everyday Italian
  Giada De Laurentiis
  2005
  30.00
  -----------------------
  Harry Potter
  J K. Rowling
  2005
  29.99
  -----------------------
  //get the value of `lang` from `title` and `version` from `year` :
   foreach (XmlNode mynode in list)
   {
       Console.WriteLine(mynode["title"].Attributes["lang"].Value );
  Console.WriteLine(mynode["year"].Attributes["version"].Value );
 Console.WriteLine("------------");
        }
//OUTPUT : 

  en
  3.5
  ----------------
  fr
  2.0

  //get books that have category="children" and print all both firstane lastname .....
  XmlNodeList list = myXmlDoc.SelectNodes("/bookstore/book[@category='children']");
    foreach (XmlNode mynode in list)
      {
   Console.WriteLine(mynode["title"].InnerText );
   Console.WriteLine(mynode["author"].InnerText);
    Console.WriteLine(mynode["price"].InnerText);
        }

//OUTPUT:
Harry Potter
J K. Rowling
29.99

//select books where the `lang` of `title` is `en`

 XmlNodeList list = myXmlDoc.SelectNodes("/bookstore/book/title[@lang='en']");
              foreach (XmlNode mynode in list)
        {
            Console.WriteLine(mynode.ParentNode["title"].InnerText );
            Console.WriteLine(mynode.ParentNode["author"].InnerText);
            Console.WriteLine(mynode.ParentNode["price"].InnerText);
        }

    //OUTPUT:

    Everyday Italian
        Giada De Laurentiis
        30.00

这篇关于如何在C#2.0 / 3.0上选择指定值的xmlelements xmlnodes xml属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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