Reading and using data from an XML file

Last post 11-09-2009 6:59 AM by nimish_soft. 6 replies.

Sort Posts:

  • Reading and using data from an XML file

    11-07-2009, 12:23 PM
    • Member
      99 point Member
    • rtcary
    • Member since 10-12-2009, 6:50 PM
    • Posts 156

    How can I read the contents of the file below and then access the <tag>, tpEdit and place the contents of <phrase> into an asp:label?  The xml file has an unknown length.  In other words, the file will be read once and some of the items will be used to fill the text fields of labels.

    <?xml version="1.0" encoding="utf-8"?>
    <items>
    <item>
        <lang>English</lang>
        <tag>btnOK</tag>
        <phrase>OK</phrase>
    </item>
    <item>
        <lang>English</lang>
        <tag>btnCancel</tag>
        <phrase>Cancel</phrase>
    </item>
    <item>
        <lang>English</lang>
        <tag>tpEdit</tag>
        <phrase>Edit claim</phrase>
    </item>
    </items>

    Todd

  • Re: Reading and using data from an XML file

    11-07-2009, 1:20 PM
    • Contributor
      3,384 point Contributor
    • Danish Ali
    • Member since 08-08-2008, 11:22 PM
    • Fort Lauderdale, US
    • Posts 468


    you can do it using following code.


    Imports System
    Imports System.Xml
    Partial Class _Default
        Inherits System.Web.UI.Page
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim doc As New XmlDocument()
            doc.Load(Server.MapPath("XMLFile.xml"))
            Dim node As XmlNode = doc.SelectSingleNode("//item[tag/text()='tpEdit']/phrase")
            If Not node Is Nothing Then
                textbox1.text = node.InnerXml
            End If
        End Sub
    End Class

    Imports System

    Imports System.Xml


    Partial Class _Default

        Inherits System.Web.UI.Page


        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


            Dim doc As New XmlDocument()

           ' for example; if you want to read the XMLFile.xml to get contents.

            doc.Load(Server.MapPath("XMLFile.xml"))

            Dim node As XmlNode = doc.SelectSingleNode("//item[tag/text()='tpEdit']/phrase")

            If Not node Is Nothing Then

                textbox1.text = node.InnerXml

            End If



        End Sub

    End Class



    If my post solves your problem, please mark it as an answer.
  • Re: Reading and using data from an XML file

    11-07-2009, 1:23 PM
    • Contributor
      2,236 point Contributor
    • mohsinaeem
    • Member since 05-08-2008, 7:41 AM
    • Fort Lauderdale, FL
    • Posts 336

    You can use ListView control to display your xml file into your well defined custom database. Because ListView control provides you full control on html. Here is a article related to ListView control binding with xml file.

    http://www.builderau.com.au/program/asp/soa/Display-data-with-NET-s-ListView-control/0,339028371,339287696,00.htm

    Mohsin Naeem
    MCPD 2.0, MCTS (SharePoint 2007, SQL Server 2005)
    "Please mark as answer if it helped you"
  • Re: Reading and using data from an XML file

    11-09-2009, 12:45 AM
    Answer

    @rtcary check out the sample I have written. Hope that helps

    string strMyXml = "<Employee>"+
                                "<firstName>"+txtFirstName.Text+"</firstName>"+
                                "<lastName>"+txtLastName.Text+"</lastName>"+
                              "</Employee>";

            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(strMyXml);
            //Here the xml is prepared and loaded in xml DOM.

            xDoc.Save(Server.MapPath("myNewXmlPerson.xml"));//instead of myNewXmlPerson , u can give any name.
            //The above line will save your XML.

            //This is one way to create xml. This method may not be very efficient to manage 500 data
            //You can go for using XmlDom instead to create xmlNodes.] Let me know if you want follow
            //xmlDom approach. To see its example you can read my blog post at
            //http://kavstech.blogspot.com/2009/07/create-xml-using-dom-and-use-xpath-to.html


            //Now if u want to read this xml and get data from it and assign their value to text box you can do like this

            txtFirstName.Text = "";
            txtLastName.Text = "";
            XmlDocument xDocRead = new XmlDocument();//I have taken this new object for your better understanding.

            xDocRead.Load(Server.MapPath("myNewXmlPerson.xml"));

            XmlNode xFN = xDocRead.SelectSingleNode("Employee/firstName");
            XmlNode xLN = xDocRead.SelectSingleNode("Employee/lastName");

            txtFirstName.Text = xFN.InnerText;
            txtLastName.Text = xLN.InnerText;

    Please mark this post as Answer if it is of help to you!

    " Every wall is a door..! "
  • Re: Reading and using data from an XML file

    11-09-2009, 5:23 AM
    • Member
      57 point Member
    • raorane.swagat
    • Member since 10-01-2009, 3:38 PM
    • mumbai
    • Posts 44

    Convert XML file to dataset and retrive appropriate value from it


    Thanks & Regards
    Swagat A. Raorane.
  • Re: Reading and using data from an XML file

    11-09-2009, 6:20 AM
    Answer
    • All-Star
      63,048 point All-Star
    • TATWORTH
    • Member since 02-04-2003, 8:34 AM
    • England
    • Posts 12,311
    • TrustedFriends-MVPs

    The XPATH /items/item[tag="tpEdit"] gives

    <item>
        <lang>English</lang>
        <tag>tpEdit</tag>
        <phrase>Edit claim</phrase>
    </item>

    /items/item[tag="tpEdit"]/phrase gives

    <phrase>Edit claim</phrase>

    Don't forget to click "Mark as Answer" on the post that helped you.
    This credits that member, earns you a point and marks your thread as Resolved so we will all know you have been helped.
  • Re: Reading and using data from an XML file

    11-09-2009, 6:59 AM
    • Participant
      1,917 point Participant
    • nimish_soft
    • Member since 10-22-2009, 9:23 AM
    • India
    • Posts 376

    fill xml in dataset...

    Dim myDataSet as New DataSet()
    myDataSet.ReadXml(Server.MapPath("books.xml"))

    - Nimish Garg
    Software Developer
    IndiaMART InterMESH Limited, Noida

    Blog: http://nimishgarg.blogspot.com/

    Plz do click "Mark as Answer" on the post that helped you. This will also give you point and help readers to know which post solved your issue and make their search easy.
Page 1 of 1 (7 items)