Reding xml node in windows Forms [modified]
-
Hi, am working on VS.net 2.0 windows application using c#. while am reading value of node in xml doc it is giving empty string.pls see the code below. can any one give me the solution where I had done mistake. XmlTextReader reader = new XmlTextReader(@"C:\book.xml"); while (reader.Read()) { reader.MoveToElement(); if (reader.Name == "year") { label1.Text = reader.Value.ToString(); } } book.xml file: <bookstore> <book category="COOKING"> <title lang="en"> Everyday Italian < /title> <author>Giada De Laurentiis </author> <year>2005 </year> <price>30.00 </price> </book> </bookstore> Thanks -- modified at 7:22 Thursday 6th September, 2007
-
Hi, am working on VS.net 2.0 windows application using c#. while am reading value of node in xml doc it is giving empty string.pls see the code below. can any one give me the solution where I had done mistake. XmlTextReader reader = new XmlTextReader(@"C:\book.xml"); while (reader.Read()) { reader.MoveToElement(); if (reader.Name == "year") { label1.Text = reader.Value.ToString(); } } book.xml file: <bookstore> <book category="COOKING"> <title lang="en"> Everyday Italian < /title> <author>Giada De Laurentiis </author> <year>2005 </year> <price>30.00 </price> </book> </bookstore> Thanks -- modified at 7:22 Thursday 6th September, 2007
You can directly read the xml file in DataSet using ds.ReadXML("xmlfilename.xml"); now you can get each Tag as Columns
Best Regards, Chetan Patel
-
Hi, am working on VS.net 2.0 windows application using c#. while am reading value of node in xml doc it is giving empty string.pls see the code below. can any one give me the solution where I had done mistake. XmlTextReader reader = new XmlTextReader(@"C:\book.xml"); while (reader.Read()) { reader.MoveToElement(); if (reader.Name == "year") { label1.Text = reader.Value.ToString(); } } book.xml file: <bookstore> <book category="COOKING"> <title lang="en"> Everyday Italian < /title> <author>Giada De Laurentiis </author> <year>2005 </year> <price>30.00 </price> </book> </bookstore> Thanks -- modified at 7:22 Thursday 6th September, 2007
XmlTextReader reader = null; try { //Load the reader with the XML file. reader = new XmlTextReader("c:\\book.xml"); //Parse the XML and display the text content of each of the elements. while (reader.Read()){ if (reader.IsStartElement()){ if (reader.IsEmptyElement) Console.WriteLine("<{0}/>", reader.Name); else{ Console.Write("<{0}> ", reader.Name); reader.Read(); //Read the start tag. if (reader.IsStartElement()) //Handle nested elements. Console.Write("\r\n<{0}>", reader.Name); Console.WriteLine(reader.ReadString()); //Read the text content of the element. } } } } finally { if (reader != null) reader.Close(); } try this code and remove space in your xml title closing tag in between < and / and put label1.text or what you like instead of Console.WriteLine . Everyday Italian !!< /title>!! <author>Giada De Laurentiis </author> <year>2005 </year> <price>30.00 </price> </book> </bookstore> Israr Ali</x-turndown>