Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
my main object is convert aspx page to xml after giving values to the textboxes,listbox. I am not able to get the values of textboxes in the xml
in below code the user has to enter values into textboxes(textbox1)
my problem is i am not getting the valueof tyextbox1. i am getting only static values means for checking pupose i gave default values to textbox2&3.
please help me.

here is my code

my aspx page:
XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox2" runat="server">text2</asp:TextBox>
        <br />
        <asp:TextBox ID="TextBox3" runat="server">text3</asp:TextBox>
        <input type="text" />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
            Text="GenerateXML" />

    </div>
    </form>
</body>
</html>

and .cs code:
method:
C#
private void GenerateXML()
    {
        string html;
        WebClient webClient = new WebClient();
        string url = "http://HtmlPagetoXML/Default2.aspx";
        using (Stream stream = webClient.OpenRead(new Uri(url)))
        
        using (StreamReader reader = new StreamReader(stream))
        {
            html = reader.ReadToEnd();
        }
        IHTMLDocument2 doc = (IHTMLDocument2)new HTMLDocument();
        doc.write(html);
        XmlDocument document = new XmlDocument();
        XmlElement root = document.CreateElement("controls");

        foreach (IHTMLElement el in doc.all)
        {
            if (el.tagName != "!" && el.tagName != "HEAD" && el.tagName != "DOCTYPE" && el.tagName != "SCRIPT" && el.tagName != "HTML" && el.tagName != "STYLE" && el.tagName != "META" && el.tagName != "LINK" && el.tagName != "TITLE" && el.tagName != "FORM" && el.tagName != "BODY")
            {
                if (el.id != null)
                {
                    if (el.getAttribute("type", 1) != null && el.getAttribute("type", 1).ToString() != "hidden")
                    {
                        XmlElement element = document.CreateElement(el.tagName);
                        // element.InnerText = el.innerText;
                        XmlElement elechild = document.CreateElement("ID");
                        elechild.InnerText = el.id;
                        element.AppendChild(elechild);
                        XmlElement elechild1 = document.CreateElement("Value");
                        XmlElement elechild2 = document.CreateElement("Type");
                        if (el.getAttribute("type", 1) != null)
                        {
                            elechild2.InnerText = el.getAttribute("type", 1).ToString();
                            element.AppendChild(elechild2);
                        }
                        if (el.getAttribute("value", 1) != null)
                        {
                            elechild1.InnerText = el.getAttribute("value", 1).ToString();
                            element.AppendChild(elechild1);
                        }
                        root.AppendChild(element);
                    }
                }
            }
        }

        string xmlfileLocation = @"D:\Test1.xml";
        XmlNode xmlNode = document.CreateNode(XmlNodeType.XmlDeclaration, "Test", "");
        document.AppendChild(root);
        document.Save(xmlfileLocation);    
    }
Posted
Updated 19-Dec-13 19:03pm
v4
Comments
Have you debugged and checked where is the problem?
vangapally Naveen Kumar 20-Dec-13 0:36am    
i checked the textbox1 values are not getting which is enter by user
Where are you reading the TextBox value?
Christopher Drake 19-Dec-13 9:23am    
Are the values prepopulated?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900