c# reading XML node quickest way...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rrayfield
    New Member
    • Nov 2007
    • 5

    c# reading XML node quickest way...

    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>Conten t Text</content>
    <H1>H1 Text/H1>
    <LogoALT>Alt Text</LogoALT>
    <Title>title text/Title>
    <Desc>Descripti on Text</Desc>
    <KeyWords>keywo rds</KeyWords>
    <Links>
    <link>
    &lt;li&gt;
    &lt;a href="Default.a spx">Company&lt ;/a&gt;
    &lt;/li&gt;
    </link>
    <link>
    &lt;li&gt;
    &lt;a href="Default.a spx">ProductsSe rvices&lt;/a&gt;
    &lt;/li&gt;
    </link>
    <link>
    &lt;li&gt;
    &lt;a href="Default.a spx">Industry News&lt;/a&gt;
    &lt;/li&gt;
    </link>
    </Links>
    </page>
    <page id="Services">
    <content>Conten t Text</content>
    <H1>H1 Text/H1>
    <LogoALT>Alt Text</LogoALT>
    <Title>title text/Title>
    <Desc>Descripti on Text</Desc>
    <KeyWords>keywo rds</KeyWords>
    <Links>
    <link>
    &lt;li&gt;
    &lt;a href="Default.a spx">Company&lt ;/a&gt;
    &lt;/li&gt;
    </link>
    <link>
    &lt;li&gt;
    &lt;a href="Default.a spx">ProductsSe rvices&lt;/a&gt;
    &lt;/li&gt;
    </link>
    <link>
    &lt;li&gt;
    &lt;a href="Default.a spx">Industry News&lt;/a&gt;
    &lt;/li&gt;
    </link>
    </Links>
    </page>
    <page id="Contact">
    <content>Conten t Text</content>
    <H1>H1 Text/H1>
    <LogoALT>Alt Text</LogoALT>
    <Title>title text/Title>
    <Desc>Descripti on Text</Desc>
    <KeyWords>keywo rds</KeyWords>
    <Links>
    <link>
    &lt;li&gt;
    &lt;a href="Default.a spx">Company&lt ;/a&gt;
    &lt;/li&gt;
    </link>
    <link>
    &lt;li&gt;
    &lt;a href="Default.a spx">ProductsSe rvices&lt;/a&gt;
    &lt;/li&gt;
    </link>
    <link>
    &lt;li&gt;
    &lt;a href="Default.a spx">Industry News&lt;/a&gt;
    &lt;/li&gt;
    </link>
    </Links>
    </page>
    </pages>

    Thanks for all your help!
    Ryan
  • Meganutter
    New Member
    • Mar 2009
    • 47

    #2
    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

    Code:
    //Load an XML File
                    XmlDocument xdXml = new XmlDocument();
                    xdXml.Load("Index.xml");
    
                    //Make a nodelist
                    XmlNodeList xnNodes = xdXml.SelectNodes("/Tools/Download");
    
                    //Walk through the list
                    foreach (XmlNode node in xnNodes)
                    {
                        if (node.FirstChild.InnerText == ddlTools.Text)
                        {
                            //Get all the child nodes
                            XmlNodeList childNodes = node.ChildNodes;
    
                            //And walk through them
                            foreach (XmlNode child in childNodes)
                            {
                                //Check which node we have now
                                switch (child.Name)
                                {
                                    case "Name":
                                        txtName.Text = child.InnerText;
                                        break;
                                    case "Version":
                                        txtVersion.Text = child.InnerText;
                                        break;
                                    case "Category":
                                        txtCategory.Text = child.InnerText;
                                        break;
                                    case "Description":
                                        txtDescription.Text = child.InnerText;
                                        break;
                                }
                            }
                            childNodes = null;
                            break;
                        }
                    }
    
                    //Clean up
                    xdXml = null;
                    xnNodes = null;

    Comment

    • despairingFreshman
      New Member
      • Jun 2009
      • 6

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

      if (node.FirstChil d.InnerText == ddlTools.Text)

      From where comes this ddlTools??
      Could please someone help me and give an explanation?

      Comment

      • Meganutter
        New Member
        • Mar 2009
        • 47

        #4
        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.

        Comment

        • despairingFreshman
          New Member
          • Jun 2009
          • 6

          #5
          Thank you :->
          Now I understand:->

          Comment

          Working...