Click here to Skip to main content
15,894,540 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a html page... I want to
1. get XML of all controls of that page
2. fill data from generated xml to all controls of that page.
I want to perform this operation using HTML and scripting language... also I want a generalize function which create XML for any form or inject any XML to controls of form..

For Example.... my page is...

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <table>
        <tr>
            <td>Name</td>
            <td>
                <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>Gender</td>
            <td>
                <asp:RadioButtonList ID="rblGender" runat="server">
                    <asp:ListItem>Male</asp:ListItem>
                    <asp:ListItem>Female</asp:ListItem>
                </asp:RadioButtonList>
            </td>
        </tr>
        <tr>
            <td>Hobbies</td>
            <td>
                <asp:CheckBoxList ID="chkHobbies" runat="server">
                    <asp:ListItem>Facebook</asp:ListItem>
                    <asp:ListItem>Whats app</asp:ListItem>
                    <asp:ListItem>Skype</asp:ListItem>
                    <asp:ListItem>Chat Messanger</asp:ListItem>
                    <asp:ListItem>Twitter</asp:ListItem>
                </asp:CheckBoxList>
            </td>
        </tr>
        <tr>
            <td>Exp.</td>
            <td>
               <asp:DropDownList ID="ddlExp" runat="server">
                   <asp:ListItem>&lt;1 year</asp:ListItem>
                   <asp:ListItem>1-2 year</asp:ListItem>
                   <asp:ListItem>2-5 year</asp:ListItem>
                   <asp:ListItem>5-10 year</asp:ListItem>
                   <asp:ListItem></asp:ListItem>
        </asp:DropDownList>
            </td>
        </tr>
        <tr>
        <td>
            <asp:Button ID="btnBlankXML" runat="server" Text="Get Blank XML"
                onclick="btnBlankXML_Click" />
            </td>
        <td><asp:Button ID="btnXMLWithData" runat="server" Text="XML with data"
                onclick="btnXMLWithData_Click" /></td>
        </tr>
    </table>
    label stRT
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    LABEL END
    </div>
    </form>
</body>
</html>


Expected XML should be....

XML
<form1>

      <txtName>Test Data</txtName>
      <rblGender>0</rblGender>
      <chkHobbies>1,3,5</chkHobbies>
      <ddlExp>2</ddlExp>
      <btnBlankXML />
      <btnXMLWithData />
      <Label1 />

  </form1>
Posted
Comments
Black Mamba Elapidae 20-Aug-15 4:59am    
From where you want to generate values in XML tags?? Example: you have mention "Test Data" in txtName xml tag, Is this something inputted in UI???
dpak chdhry 25-Aug-15 2:03am    
yes "Test Data" is a entered by user from UI in txtName

1 solution

Hey you can use below function to Generate xml for controls. In the below code I have displayed xml in new window . You can modify the code to make use of that xml however you want. I haven't added values in the xml tags as in question you haven't mentioned about values in details.

C#
function displayFormData() {
      NewWindow = open("", "window2")
      NewWindow.document.open("text/plain")
      var i = 0;
      var j = 0;
      var x = "";
      for (i = 0; i < document.forms.length; ++i) {
          x = "<" + document.forms[i].id + ">";
          for (j = 0; j < document.forms[i].elements.length; ++j) {
              x = x + "<" + document.forms[i].elements[j].id + ">";
              x = x + "</" + document.forms[i].elements[j].id + ">";
          }
          x = x + "</" + document.forms[i].id + ">";
          NewWindow.document.write(x);
      }
      NewWindow.document.close()
      return false
  }


Tell me if you need any further help on this one !!
Hope this helps :-)
 
Share this answer
 

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