Click here to Skip to main content
16,016,736 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
hi
C# code for reading and displaying data from xml file
i am new to the IT industry.. team leader told me to create a xml file and display it.. what can i do.. below file is my xml file,

HTML
<book>
  <bookname>C# Professional</bookname>
  <bookname>C# Cookbook</bookname>
  <bookname>SQL Server Black Book</bookname>
  <bookname>Mastering VB.Net</bookname>
  <bookname>ASP.Net Unleashed</bookname>
  <bookname>.Net Framework Essentials</bookname>
  <releaseyear>2001</releaseyear>
  <releaseyear>2002</releaseyear>
  <releaseyear>2003</releaseyear>
  <releaseyear>2004</releaseyear>
  <publication>EEE</publication>
  <publication>Microsoft Press</publication>
  <publication>O 'Reilly</publication>
  <publication>BpB</publication> 
  <publication>Sams TechMedia</publication>
</book>
Posted
Updated 10-Nov-11 20:48pm
v4

XML is already a readable form. You could make it more readable by re-formatting or adding formatting to the text. For example, you can present the text in thoroughly formatted HTML with colors, indentation, etc. One of the "standard" ways to do it is XSLT.

See:
http://en.wikipedia.org/wiki/XSLT[^],
http://www.w3.org/TR/xslt20/[^],
http://www.nwalsh.com/docs/tutorials/xsl/xsl/[^],
http://www.dpawson.co.uk/xsl[^],
http://www.w3schools.com/xml/xml_xsl.asp[^].

—SA
 
Share this answer
 
v2
Comments
Mehdi Gholam 11-Nov-11 1:59am    
XML is pretty readable, although the signal to noise is low :)
5'ed
Sergey Alexandrovich Kryukov 11-Nov-11 2:09am    
Exactly! Nevertheless, my advice could also work to remove some of noise. I've developed some XSLT; it's quite powerful, but writing XSLT code is not so easy.

Thank you, Mehdi.
--SA
This is link will help you more

http://www.functionx.com/csharp2/xml/Lesson03.htm[^]
 
Share this answer
 
Comments
afsal.mp 11-Nov-11 2:30am    
thank u.....rajesh
Rajesh Anuhya 11-Nov-11 2:38am    
Welcome..
 
Share this answer
 
Comments
afsal.mp 11-Nov-11 2:30am    
thank u uday
You can do this by different ways..
Actual what is the type of above mentioned XML,
is it in String type or XmlDocument type..?
And To Which this Xml data is to convert? object type or you want to select each individual tags..?

1)If the above file is in String type, then it is better to convert that to Object type,
Create a class called Book
and create properties to that class with the same name in the each tag ex:BookName
Called Xml Deserialization

SQL
XML Deserialization:
Deserialization is the reverse process. We will load in an xml, pass the data to the deserializer and it produce an instance of the class populated with the data.

HTML
XML Serialization:
XML Serialization is the process of saving class member data into an XML. Only public classes and public properties and fields can be serialised – methods and private members are not serialized and cannot be deserialized. Private classes cannot be serialized and will result in a compilation error.


C#
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public class Book
    {
        public string BookName { get; set; }
        public string ReleaseYear { get; set; }
        public string Publication { get; set; }
    }



C#
public List<Book> ConvertXmlToList()
       {
           List<Book> list = new List<Book>();
           using (var sr = new StreamReader(@"c:\test.xml"))
           {
               var deserializer = new XmlSerializer(typeof(List<Book>));
               list = (List<Book>)deserializer.Deserialize(sr);
           }
           return list;
       }
 
Share this answer
 
Comments
afsal.mp 11-Nov-11 2:42am    
i am new to the IT industry.. team leader told me to create a xml file and display it.. what can i do.. above file is my xml file,

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