By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,661 Members | 781 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,661 IT Pros & Developers. It's quick & easy.

c# reading XML node quickest way...

P: 5
I have a XML file that contains content for an asp.net website. I need the quickest way to find the node and write the elements out to the page. Also how would I get the links section out of it? do I have to loop through them? Example = I need the content for the Home Page, and I have the id=Home in the xml, how do I get the rest of the data to write out to the page in that node between <page id="Home"> and </page>

<?xml version="1.0" encoding="utf-8"?>
<pages>
<page id="Home">
<content>Content Text</content>
<H1>H1 Text/H1>
<LogoALT>Alt Text</LogoALT>
<Title>title text/Title>
<Desc>Description Text</Desc>
<KeyWords>keywords</KeyWords>
<Links>
<link>
&lt;li&gt;
&lt;a href="Default.aspx">Company&lt;/a&gt;
&lt;/li&gt;
</link>
<link>
&lt;li&gt;
&lt;a href="Default.aspx">ProductsServices&lt;/a&gt;
&lt;/li&gt;
</link>
<link>
&lt;li&gt;
&lt;a href="Default.aspx">Industry News&lt;/a&gt;
&lt;/li&gt;
</link>
</Links>
</page>
<page id="Services">
<content>Content Text</content>
<H1>H1 Text/H1>
<LogoALT>Alt Text</LogoALT>
<Title>title text/Title>
<Desc>Description Text</Desc>
<KeyWords>keywords</KeyWords>
<Links>
<link>
&lt;li&gt;
&lt;a href="Default.aspx">Company&lt;/a&gt;
&lt;/li&gt;
</link>
<link>
&lt;li&gt;
&lt;a href="Default.aspx">ProductsServices&lt;/a&gt;
&lt;/li&gt;
</link>
<link>
&lt;li&gt;
&lt;a href="Default.aspx">Industry News&lt;/a&gt;
&lt;/li&gt;
</link>
</Links>
</page>
<page id="Contact">
<content>Content Text</content>
<H1>H1 Text/H1>
<LogoALT>Alt Text</LogoALT>
<Title>title text/Title>
<Desc>Description Text</Desc>
<KeyWords>keywords</KeyWords>
<Links>
<link>
&lt;li&gt;
&lt;a href="Default.aspx">Company&lt;/a&gt;
&lt;/li&gt;
</link>
<link>
&lt;li&gt;
&lt;a href="Default.aspx">ProductsServices&lt;/a&gt;
&lt;/li&gt;
</link>
<link>
&lt;li&gt;
&lt;a href="Default.aspx">Industry News&lt;/a&gt;
&lt;/li&gt;
</link>
</Links>
</page>
</pages>

Thanks for all your help!
Ryan
Mar 3 '09 #1
Share this Question
Share on Google+
4 Replies


P: 47
I just started out with XML files and found the XmlNodeList function quite usable.

heres a snippet of my code, i hope it makes sense to you

Expand|Select|Wrap|Line Numbers
  1. //Load an XML File
  2.                 XmlDocument xdXml = new XmlDocument();
  3.                 xdXml.Load("Index.xml");
  4.  
  5.                 //Make a nodelist
  6.                 XmlNodeList xnNodes = xdXml.SelectNodes("/Tools/Download");
  7.  
  8.                 //Walk through the list
  9.                 foreach (XmlNode node in xnNodes)
  10.                 {
  11.                     if (node.FirstChild.InnerText == ddlTools.Text)
  12.                     {
  13.                         //Get all the child nodes
  14.                         XmlNodeList childNodes = node.ChildNodes;
  15.  
  16.                         //And walk through them
  17.                         foreach (XmlNode child in childNodes)
  18.                         {
  19.                             //Check which node we have now
  20.                             switch (child.Name)
  21.                             {
  22.                                 case "Name":
  23.                                     txtName.Text = child.InnerText;
  24.                                     break;
  25.                                 case "Version":
  26.                                     txtVersion.Text = child.InnerText;
  27.                                     break;
  28.                                 case "Category":
  29.                                     txtCategory.Text = child.InnerText;
  30.                                     break;
  31.                                 case "Description":
  32.                                     txtDescription.Text = child.InnerText;
  33.                                     break;
  34.                             }
  35.                         }
  36.                         childNodes = null;
  37.                         break;
  38.                     }
  39.                 }
  40.  
  41.                 //Clean up
  42.                 xdXml = null;
  43.                 xnNodes = null;
Mar 4 '09 #2

P: 6
I am not too experienced and try understanding this code but I am hanging with (Line 11):

if (node.FirstChild.InnerText == ddlTools.Text)

From where comes this ddlTools??
Could please someone help me and give an explanation?
Jun 4 '09 #3

P: 47
ddlTools is a dropdown list in the application, the parts that matter are the foreach loops mostly.

basically it checks wether the XML value is the same as the selected value of that dropdown menu.
Jun 4 '09 #4

P: 6
Thank you :->
Now I understand:->
Jun 8 '09 #5

Post your reply

Sign in to post your reply or Sign up for a free account.