Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi for Exampel

<pre lang="xml"><?xml version="1.0" encoding="utf-8" ?>
- <ARI>
- <Unit ID="1" Name="آذربایجان شرقی">
  <city ID="1">آذرشهر</city>
  <city ID="2">اسکو</city>
  <city ID="3">اهر</city>
  <city ID="4">بستان‌آباد</city>
  <city ID="5">بناب</city>
  <city ID="6">تبریز</city>
  <city ID="7">جلفا</city>
  <city ID="8">چاراویماق</city>
</Unit>
- <Unit ID="2" Name="آذربایجان غربی">
  <city ID="1">ارومیه</city>
  <city ID="2">اشنویه</city>
  <city ID="3">بوکان</city>
  <city ID="4">پیرانشهر</city>
  <city ID="5">تکاب</city>
  <city ID="6">چالدران</city>
  <city ID="7">خوی</city>
  <city ID="8">خوی</city>
  <city ID="9">سردشت</city>
  <city ID="10">سلماس</city>
  <city ID="11">شاهین‌دژ</city>
  <city ID="12">ماکو</city>
  <city ID="13">مهاباد</city>
  <city ID="14">میاندوآب</city>
  <city ID="15">نقده</city>
  </Unit>

</ARI>




1-how read this file and show list city of unit with id 1 forexample?
2-how show name and id unit with id =1?
Posted
Comments
Kim Togo 13-Jun-11 13:00pm    
Is this for ASP.NET, Winform or WPF ?
Or are you only interesting in finding info in XML ?

Here are the options to consider:


  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


—SA
 
Share this answer
 
Comments
Kim Togo 14-Jun-11 2:37am    
Good options SA. My 5.
Sergey Alexandrovich Kryukov 14-Jun-11 3:01am    
Thank you, Kim.
--SA
If you are not restricted to C# < 3.5. I would use LINQ to XML[^] to that type of query. Very easy to use.

C#
XElement xml = XElement.Load(@"c:\xml\data.xml");
var result = from units in xml.Element("ARI").Elements("Unit")
             from city in units.Elements("city")
             where city.Attribute("ID").Value == "1"
             select city;
 
Share this answer
 
Comments
NandaKumer 13-Jun-11 12:18pm    
nice one.
Kim Togo 13-Jun-11 12:57pm    
Thanks Nanda
yesotaso 13-Jun-11 13:45pm    
Indeed easist way and cleanest way if the restriction does not apply... My 5.
Kim Togo 14-Jun-11 2:36am    
Thanks
RakeshMeena 14-Jun-11 1:31am    
Nice one.
 
Share this answer
 
DataSet ds = new DataSet();
ds.ReadXml (MapPath("books.xml"));
DataGrid1.DataSource = ds;
DataGrid1.DataBind();


Or have alook in the following link,-

http://www.stardeveloper.com/articles/display.html?article=2009030701&page=1[^]

http://www.w3schools.com/aspnet/aspnet_repeater.asp[^]
 
Share this answer
 
Comments
Kim Togo 13-Jun-11 12:59pm    
But this example has nothing to do with selecting/searching nodes/elements in XML. And where do ASP.NET get from?
OP has never mention anything for on ASP.NET. My vote of 1.

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