Run Usb
Run Usb

Reputation: 1

How can I display XML data using C#

I am trying to display the id attribute of the channel element called id, the inner text of the display-name tag and the inner text of the icon that sometimes is contained inside the channel element.

 <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE tv SYSTEM "xmltv.dtd">
<tv generator-info-name="xmltv.co.uk" source-info-name="xmltv.co.uk">
  <channel id="0052a71acac348ff93f5680aa9c125eb">
    <display-name>2910</display-name>
  </channel>
  <channel id="00da025711e82cf319cb488d5988c099">
    <display-name>Sony Movies</display-name>
  </channel>
  <channel id="00dfea977320f17bb419abaa1f079f39">
    <display-name>Good Food</display-name>
    <icon src="/images/channels/00dfea977320f17bb419abaa1f079f39.png"/>
  </channel>
<channel id="018202232e044b504f9dc5263617d496">
    <display-name>The Box</display-name>
    <icon src="/images/channels/018202232e044b504f9dc5263617d496.png"/>
  </channel>

I tried using this code C# code below But the second if give me a error about not referenced to an object.

 XmlDocument doc = new XmlDocument();

        doc.Load(xmlLocation);



        //dispaly the  nodes
        foreach (XmlNode node in doc.DocumentElement.ChildNodes) 
        {

            //get the channel
            if (node.Name.Equals("channel"))
            {

                Debug.WriteLine("Channel Name : " + node.ChildNodes[0].Name.ToString()); //or loop through its children as well
                //Debug.WriteLine("Channel Name : " + node.AttributeCount.ToString()); //or loop through its children as well

//get the icon element
                  if(node.ChildNodes[1].Name != null)
                    Debug.WriteLine("Channel Name : " + node.ChildNodes[1].Name.ToString());


            }

        }

Upvotes: 0

Views: 191

Answers (2)

Sreenath M
Sreenath M

Reputation: 1

First, you need to convert string to XML and load them up in XmlDocument and then use the XPath as shown below. The simple program you can run that in dotnetfiddle.net to check this out.

using System;
using System.Xml;

public class Program
{
    public static void Main()
    {
        string xmlString = "<tv generator-info-name='xmltv.co.uk' source-info-name='xmltv.co.uk'>   <channel id='0052a71acac348ff93f5680aa9c125eb'> <display-name>2910</display-name>   </channel>   <channel id='00da025711e82cf319cb488d5988c099'>     <display-name>Sony Movies</display-name>   </channel>   <channel id='00dfea977320f17bb419abaa1f079f39'>     <display-name>Good Food</display-name>     <icon src='/images/channels/00dfea977320f17bb419abaa1f079f39.png'/>   </channel> <channel id='018202232e044b504f9dc5263617d496'>     <display-name>The Box</display-name>     <icon src='/images/channels/018202232e044b504f9dc5263617d496.png'/>   </channel></tv>";
        XmlDocument xmltest = new XmlDocument();
        xmltest.LoadXml(xmlString);
        XmlNodeList itemNodes = xmltest.SelectNodes("//tv/channel");
        foreach(XmlNode itemNode in itemNodes)
        {
                if (itemNode!= null) {
            Console.WriteLine(string.Format("Id:{0}", (itemNode as XmlElement).GetAttribute("id")));
            }
        }
    }

}

Upvotes: 0

Oguz Ozgul
Oguz Ozgul

Reputation: 7187

Although XDocument/XElement and LinQ to XML is the new trend,

to follow your implementation, and adding to it only one feature (using XPATH to query document contents);

Please find the code to fetch channel names and their respective icon source URL's (if exist)

By applying SelectNodes and SelectSingleNode, the API is iterating over the nodes for us.

// Select all the XML elements whose name is "channel"
foreach (XmlNode channelNode in doc.DocumentElement.SelectNodes("channel"))
{
    // check if a child element with the name "display-name" exists
    XmlNode displayNameNode = channelNode.SelectSingleNode("display-name");
    if (displayNameNode != null)
    {
        // If yes, print the inner text
        Debug.WriteLine("Channel Name : " + displayNameNode.InnerText);
    }

    // then check if the icon node exists
    XmlNode iconNode = channelNode.SelectSingleNode("icon");

    if (iconNode != null)
    {
        // and check if it has an attribute with the name "src"
        if (iconNode.Attributes["src"] != null)
        {
            // and if yes, print out its value
            Debug.WriteLine("    Icon Src : " + iconNode.Attributes["src"].Value);
        }
    }
}

Upvotes: 1

Related Questions