Retriving data from xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raghulvarma
    New Member
    • Oct 2007
    • 90

    Retriving data from xml file

    My XMLFile is as shown below
    Code:
    <?xml version="1.0" encoding="utf-8"?>
     <root>
    <BookDetails> 
        <bookid>156</bookid>
         <bookname>xml</bookname>
         <author>rajendran</author>
    </BookDetails> 
    <BookDetails> 
        <bookid>789</bookid> 
        <bookname>ajax</bookname>
         <author>kumaran</author>
    </BookDetails>
    </root>
    Now if I need to retrive all the bookid alone and if i need to display that in a dropdownlist then how should I do in asp.net using C#?
    and in the same wise after making the selected index changed in dropdownlist how should I retrive the particular details of bookid and display that in the textbox?

    please help me?
    I do not have clear cut idea how to proceed.
    thanks in advance

    regards
    raghul
  • coolcode
    New Member
    • Aug 2008
    • 6

    #2
    Hi Raghu I have coded the following ASP.Net C# code for you to bind the xml data file with DropDownList control.

    SelectedIndexCh anged event of the DropDownList is used in the C# code to bind the corresponding properties values of selected bookId.

    HTML Code:

    Code:
            <div>
                Select BookID:
                <asp:DropDownList ID="drdBooksId" runat="server" AutoPostBack="True" OnSelectedIndexChanged="drdBooksId_SelectedIndexChanged">
                </asp:DropDownList><br />
                <br />
                BookName:
                <asp:TextBox ID="txtBookName" runat="server"></asp:TextBox><br />
                <br />
                Author:
                <asp:TextBox ID="txtAuthor" runat="server"></asp:TextBox>
            </div>


    C# Code Behind:

    Code:
    
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDrdBooksId();
            }
        }
    
        public void BindDrdBooksId()
        {
            DataSet xmlDataSet = new DataSet();
    
            // In ASP.Net use App_Data Directory for xml data files
            xmlDataSet.ReadXml(Server.MapPath("../App_Data/books.xml"));
    
            // if xml file is in the same directory
            // xmlDataSet.ReadXml("books.xml");
    
            drdBooksId.DataSource = xmlDataSet;
            drdBooksId.DataValueField = "bookid";
            drdBooksId.DataTextField = "bookid";
            drdBooksId.DataBind();
            
        }
    
        protected void drdBooksId_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindXmlData(drdBooksId.SelectedItem.Value);
        }
    
        public void bindXmlData(string bookID)
        {
            DataSet xmlDataSet = new DataSet();
    
            // In ASP.Net use App_Data Directory for xml data files
            xmlDataSet.ReadXml(Server.MapPath("../App_Data/books.xml"));
    
            // if xml file is in the same directory
            //xmlDataSet.ReadXml("books.xml");
    
            DataRow[] dRow = xmlDataSet.Tables[0].Select("bookid=" + bookID);
    
            txtBookName.Text = dRow[0]["bookName"].ToString();
            txtAuthor.Text = dRow[0]["author"].ToString();
        }

    I saved the provided xml schema and dummy xml data file in the App_Data. You can try this code, I hope this will help you.

    Good Luck ;-)

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      coolcode:
      In the future, please try to avoid giving other members fully coded solutions. On this forum, we want to promote leaning and understanding. While sometimes the best way to do that is with a code sample, copy-paste code teaches nothing other than how to copy and paste. So perhaps suggest the namespaces and objects to look into, or post a code sample that can't be directly copied and pasted, but please don't do other members assignments/homework for them.

      MODERATOR

      Comment

      • coolcode
        New Member
        • Aug 2008
        • 6

        #4
        Originally posted by insertAlias
        coolcode:
        In the future, please try to avoid giving other members fully coded solutions. On this forum, we want to promote leaning and understanding. While sometimes the best way to do that is with a code sample, copy-paste code teaches nothing other than how to copy and paste. So perhaps suggest the namespaces and objects to look into, or post a code sample that can't be directly copied and pasted, but please don't do other members assignments/homework for them.

        MODERATOR
        Yea sure. Next time I will keep in mind to provide only logic behind the code rather than complete solution. This solution is my first contributing post.

        Comment

        • Curtis Rutland
          Recognized Expert Specialist
          • Apr 2008
          • 3264

          #5
          We're glad to have you here contributing.

          Comment

          Working...