Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
here my code
is

and how to get column names insted of indexvalues

C#
public static void savefile_XML(string EmpId, string FirstName, string LastName, string MiddleName, string Gender, string DateOfBirth, string MobileNo, string EmailId, string Adress, string District, string State, string Country, string zip)
    {
        
        try
        {
           

                string[] arr1 = new string[] { EmpId, FirstName, LastName, MiddleName, Gender, DateOfBirth, MobileNo, EmailId, Adress, District, State, Country, zip };
                XmlDocument xmlDoc = new XmlDocument();
                XmlElement element = xmlDoc.CreateElement("data");

                for (int i = 0; i < arr1.Length; i++)
                {
                    XmlElement element1 = xmlDoc.CreateElement((i).ToString()); ;
                    element1.InnerText = arr1[i];
                    element.AppendChild(element1);
                    xmlDoc.AppendChild(element);



                }
               
                xmlDoc.Save(@" F:\srikanth\jqgrid\test.xml");
            
 }

        catch (Exception ex)
        {
            Elmah.ErrorLog.GetDefault(HttpContext.Current).Log(new Elmah.Error(ex, HttpContext.Current));
        }




here iam getting like this
XML
<0>2020</0>
  <1>narendra</1>
  <2>modi</2>
  <3>n</3>


iwant numbers insted of column names can u tell the code .....and how to save that file into our current project ...and how to download saved file ..plz help ...i dont no xml..iam new to the xml........
Posted
Updated 19-Jun-14 0:20am
v3
Comments
Kareem Abou Saad 18-Jun-14 4:44am    
Probably because you are using xmlDoc.CreateElement((i).ToString()) ...
srikanth492 18-Jun-14 7:02am    
thanks for replay..
srikanth492 18-Jun-14 4:51am    
hi how to get column names insted of numbers can u tell me iam using xml and webmethod c#
srikanth492 18-Jun-14 6:41am    
i save xml file into my project like this...Guid g;
g= Guid.NewGuid();
//g = GuidNewGuid();

var Mappingpath = HttpContext.Current.Server.MapPath(@".\");
xmlDoc.Save(Mappingpath +EmpId + g +".xml"); and how to download when click download button................
srikanth492 23-Jun-14 14:39pm    
how to download when click the download button i can get entire row in pdf format downloaded in web browser i know code .see below
string filePath = ConfigurationManager.AppSettings["myFilePath"].ToString();
Guid g;
g = Guid.NewGuid();
string strFilename = EmpId + g + ".xml";
string Mappingpath = HttpContext.Current.Server.MapPath(filePath + strFilename);
xmlDoc.Save(Mappingpath);
return filePath + strFilename;
in this code one problem is there ...iam using static webpage when click the download it will just show values we only manuvally save(ctrl+s)....how can over come iam using jquery and json............this is my ajax code .....$.ajax({
type: "POST",
url: "EmployeeDetails.aspx/savefile_XML",
contentType: "application/json; charset=utf-8",
dataType: "json",
data: "{'getdata':" + JSON.stringify(getdata) + "}",
success: function (msg) {
window.open(msg.d, "Employyees");
var link = document.createElement("EmployeeDetails");
var fileName = data.substring(data.lastIndexOf('/') + 1);
link.download = fileName;
link.href = data;
link.click();
alert("downloaded");
},

Instead of using
CSS
CreateElement((i).ToString());

Use
CreateElement((tagName));

Where tagName could be for example FirstName or LastName, so it ends up in the XML tag as
XML
<firstname></firstname>

To do this in the simplest way, create an array of fields holding literal strings like this:
C#
string[] fields = {"FirstName", "LastName", "Id"}; //etc..

Replace the
C#
(i).ToString()
with
C#
fields[i]

This should solve your problem.
 
Share this answer
 
Comments
srikanth492 18-Jun-14 6:59am    
i got like this <tagname>2017
srikanth492 18-Jun-14 7:10am    
<tagname>Male
srikanth492 18-Jun-14 7:00am    
i write code like this ..XmlElement element1 = xmlDoc.CreateElement(("tagName"));
element1.InnerText = fields[i];
solved my self using this code ....iam geting column name with help of this code.....


string EmpId = string.Empty;
var objects = JArray.Parse(getdata);
XmlDocument xmlDoc = new XmlDocument();
XmlElement element = xmlDoc.CreateElement("details");

foreach (JObject root in objects)
{
foreach (KeyValuePair<string,> app in root)
{
var appName = app.Key;//in this app.key getting column name
EmpId = (String)app.Value;// we can get empid
XmlElement element1 = xmlDoc.CreateElement(appName);
element1.InnerText = (String)app.Value;
element.AppendChild(element1);
xmlDoc.AppendChild(element);
}
}
 
Share this answer
 
Comments
srikanth492 23-Jun-14 14:43pm    
can i know the the reason downvoted? this code works perfectly

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