how to select xmlelements xmlnodes xmlattributes of specified values on c# 2.0/3.0

和自甴很熟 提交于 2019-12-11 19:32:20

问题


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-how to select all books and print their element's text (title,author,...) ?

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

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

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


回答1:


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


来源:https://stackoverflow.com/questions/20078780/how-to-select-xmlelements-xmlnodes-xmlattributes-of-specified-values-on-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!