Click here to Skip to main content
15,898,978 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can i display an xml content in a label the following is the xml content
<?xml version="1.0" encoding="utf-8" ?>
<Books>
    <Text>
       <Subject>
           Alpha
       </Subject>
       <Content>
           Bravo
       </Content>
    </Text>
<Books>


I want to display the content as

Subject:Alpha
Content:Bravo


in two different labels
Posted
Updated 13-Jan-11 22:34pm
v2

Write a LINQ similar to this & try

C#
// Create the query
            var books = from c in System.Xml.Linq.XElement.Load("Books.xml").Elements("Text")
                        select c;
            if (books != null)
            {
                foreach (var b in books)
                {

                    string subjectvalue = b.Element("Subject").Value; //will give you Alpha
                }
            }
 
Share this answer
 
Comments
Espen Harlinn 14-Jan-11 9:20am    
5+ Yes, that's an easy workable solution
Anupama Roy 16-Jan-11 4:29am    
Thanks Espen.
So what problem you are facing, You need to parse the XML and access the appropriate nodes and assign it to Label's text property.

You can query to your XML using XPath. Have a look to the link for example
 
Share this answer
 
You may use something like
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load("mydata.xml");
label1.Text = xmldoc.SelectSingleNode("/Books/Text/Subject").FirstChild.Value;
label2.Text = xmldoc.SelectSingleNode("/Books/Text/Content").FirstChild.Value;


Provided you correctly fix your XML document (the bottom node should be </Books>)
 
Share this answer
 
Create an xml schema for your xml file and use xsd.exe[^] to generate code for the schema.

Use generated code to deserialize[^] contents of xml file. At this point you have a collection representing you data in memory.

Use the Data Repeater[^] to create your ui.

Regards
Espen Harlinn
 
Share this answer
 
v2
In addition to the excellent answers already given, you could look into xsl transformations :
http://www.w3schools.com/xsl/[^]

Cheers
 
Share this answer
 

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