troubleshooting Question

javascript to xml

Avatar of ITsolutionWizard
ITsolutionWizardFlag for United States of America asked on
JavaScriptC#XML
2 Comments1 Solution40 ViewsLast Modified:
I have the following codes to loop each of the item within the html form
and inside of the javascript. I want to generate xml string like
<xml>
<tranid id="test">
<firstname>John</firstname>
<lastname>Smiths</lastname>
</tranid>
</xml>

1. How can I do that in javascript?
2. When the xml string is passed back to c# code behind, will it create any security issue because of xml tag?

<div class="12u">
                    <div class="row"> 
                    <div class="6u"> 
                    <label id="lblfirstname" for="firstname"></label>
                    <input type="text" id="firstname" placeholder="first name" required />
                    </div>
                    <div class="6u"> 
                    <label id="lbllastname" for="lastname"></label>
                    <input type="text" id="lastname" placeholder="last name" />
                    </div>
                    </div>
                    <label id="lblcompanyname" for="companyname"></label>
                    <input type="text" id="companyname" placeholder="company name" />

                    <label id="lbladdress" for="address"></label>
                    <input type="text" id="address" placeholder="address" />

                    <div class="row"> 
                    <div class="6u"> 
                    <label id="lblphoneno" for="phoneno"></label>
                    <input type="text" id="phoneno" placeholder="phone number"  />
                        </div>
                    <div class="6u"> 
                    <label id="lblemail" for="email"></label>
                    <input type="text" id="email" placeholder="email" />
                    </div>
                    </div> 
</div>
<script type="text/javascript">        
        function submit()
        {       
        var str = '';
        var elem = document.getElementById('aspnetForm').elements;
        for(var i = 0; i < elem.length; i++)
        {
          str += elem[i].id + ":" + elem[i].value + "&";
        } 
        $.ajax({
        type: "POST",
        data: '{namestring:"' + str + '"}',
        url: "~/redapp.aspx/PostData",        
        contentType: "application/json; charset=utf-8",         
        dataType: "json",
        success: OnSuccess,
        failure: function(response) {
            alert(response.d);
        }
        });
        }
        function OnSuccess(response) {
         alert("Success " + response.d);
        }
        function DisplayFormValues()
        {
        var str = '';
        var elem = document.getElementById('aspnetForm').elements;
        for(var i = 0; i < elem.length; i++)
        {
            str += elem[i].id + ":" + elem[i].value + "\n";
        } 
        alert(str);
        }
</script>
    [System.Web.Services.WebMethod]
    public static string PostData(string namestring)
    {
        return "Success";
    }
ASKER CERTIFIED SOLUTION
ste5an
Senior Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros