Friday, May 20, 2011

How to read and return an XML in asp.net

public XmlTextReader XMLparse()
    {
        XmlTextReader m_xmlr = null;
        //Create the XML Reader
        m_xmlr = new XmlTextReader("D:\\Work\\GetExcelSheetNames\\XMLFile.xml");
        //Disable whitespace so that you don't have to read over whitespaces
        m_xmlr.WhitespaceHandling = WhitespaceHandling.None;
        //read the xml declaration and advance to family tag
        m_xmlr.Read();
        //read the family tag
        m_xmlr.Read();
        //Load the Loop
        while (!m_xmlr.EOF)
        {
            //Go to the name tag
            m_xmlr.Read();
            //if not start element exit while loop
            if (!m_xmlr.IsStartElement())
            {
                break; // TODO: might not be correct. Was : Exit While
            }
            //Get the Gender Attribute Value
            string genderAttribute = m_xmlr.GetAttribute("gender");
            //Read elements firstname and lastname
            m_xmlr.Read();
            //Get the firstName Element Value
            string firstNameValue = m_xmlr.ReadElementString("firstname");
            //Get the lastName Element Value
            string lastNameValue = m_xmlr.ReadElementString("lastname");
            //Write Result to the Console
            Response.Write("Gender: " + genderAttribute + " FirstName: " + firstNameValue + " LastName: " + lastNameValue);
            // Console.Write(Constants.vbCrLf);
        }
        //close the reader
        m_xmlr.Close();
        return m_xmlr;
    }

No comments:

Post a Comment