Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 5th, 2007, 05:05 AM
Authorized User
 
Join Date: Nov 2006
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Somesh
Default Problem in adding the new element into the XML fil

Hi,
All

I am trying to add the new element(Node) into the XML document. Atfirst the code is cutting the columns headers of a gidview then it is storing the columns elements into an array, then I need to write the array elements into an XML file.

 When I am calling the XMLWrite method it is writting the first array element. Then I need to add the others array element.

I am giving the code also.

protected void Button2_Click(object sender, EventArgs e)
    {
        int abc;
        abc = GridView1.Columns.Count;
        TextBox1.Text = abc.ToString();
        string[] MyArray;
        MyArray = new string[abc];
        int m = abc;
        int j = 0;
        int i = 0;
        for (i = 0; i < m; i++)
        {
            string Clnt = GridView1.HeaderRow.Cells[j].Text;

            MyArray[j] = Clnt;
            j = j + 1;
        }
        //string xmlFilePath = @"C:\Data\Employees.xml";
        string xmlFilePath = @"C:\Documents and Settings\somesh.c.BSSAS0\My Documents\Visual Studio 2005\WebSites\WebSite10\Employees.xml";

        try
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.ConformanceLevel = ConformanceLevel.Auto;
            settings.IndentChars = "\t";
            settings.OmitXmlDeclaration = false;
            using (XmlWriter writer = XmlWriter.Create(xmlFilePath, settings))
            {
                //Start writing the XML document
                writer.WriteStartDocument(true);
                //Start with the root element
                i = 0;
                for (i = 0; i < m; i++)
                {
                    string get;
                    get = MyArray[i];
                   writer.WriteStartElement("Element", get);
                    writer.WriteEndElement();
                    writer.WriteEndDocument();
                    //Flush the object and write the XML data to the file
                    //writer.Flush();

                }


            }
        }


        catch (Exception ex)
        {
            Response.Write("An Exception occurred: " + ex.Message);
        }


    }
}


If any one can solve it then it will be a great help for me.

With regards

Somesh Chatterjee.
__________________
somesh chaterjee
 
Old February 5th, 2007, 03:17 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You need to write more elements using the "WriteStartElement" and "WriteEndElement" methods. Perhaps we could help more if you can provide a simple example of the the data you are getting, and what the resulting XML should look like.

-Peter
 
Old February 6th, 2007, 12:59 AM
Authorized User
 
Join Date: Nov 2006
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Somesh
Default

Hi,
At first, I want to give you, thank for showing interest about my posting.
I am sending the sample data for Example.

Remember my last post, you will found there is an array the array is not fixed size. According to the size of the Grid view column header the array, size will set programmatically. For example, say there is five header (or Array elements). First one - EmployeeID, second one- Employee First Name, than Last name, designation, Skill accordingly.

Now when I am starting to write the XML file then I don’t know that what is the array size that's why I need to write the first element then a for loop will add the element as the child node of the first element.


I am able to create the XML file and write the first element but till unable to add the child node by for loop.

If you can solve it, I shall be highly obliged.

Than you

Somesh


someshchaterjee
 
Old February 6th, 2007, 06:41 AM
Authorized User
 
Join Date: Nov 2006
Posts: 47
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Somesh
Default

Hi, All

Now I am able to add the new elements into the XML file.

i am giving the code through which I did id

    protected void Button2_Click(object sender, EventArgs e)
    {
        int abc;
        abc = GridView1.Columns.Count;
        TextBox1.Text = abc.ToString();
        string[] MyArray;
        MyArray = new string[abc];
        int m = abc;
        int j = 0;
        int i = 0;
        for (i = 0; i < m; i++)
        {
            string Clnt = GridView1.HeaderRow.Cells[j].Text;

            MyArray[j] = Clnt;
            j = j + 1;
        }
        //string xmlFilePath = @"C:\Data\Employees.xml";
        string xmlFilePath = @"C:\Documents and Settings\somesh.c.BSSAS0\My Documents\Visual Studio 2005\WebSites\WebSite10\Employees.xml";

        try
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            settings.ConformanceLevel = ConformanceLevel.Auto;
            settings.IndentChars = "\t";
            settings.OmitXmlDeclaration = false;
            using (XmlWriter writer = XmlWriter.Create(xmlFilePath, settings))
            {
                //Start writing the XML document
                writer.WriteStartDocument(true);
                //Start with the root element
                i = 0;
                string get;
                get = MyArray[i];

                writer.WriteStartElement("employees");
                writer.WriteStartElement("employee");
                writer.WriteStartElement("attribute");
                //writer.WriteStartElement(typeof 'CheckBox');
                writer.WriteElementString("element", get);

                i = 1;
                for (i = 1; i < m; i++)
                {
                    get = MyArray[i];
                    writer.WriteElementString("element", get);
                }


                writer.WriteEndElement();
                writer.WriteEndElement();
                writer.WriteEndDocument();

            }

        }
        catch (Exception ex)
        {
            Response.Write("An Exception occurred: " + ex.Message);

        }


    }



Thanks

Somesh.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
Adding a new Element monuindia2002 XML 2 March 13th, 2006 11:59 PM
restrict the uploaded file to ACCEPT ONLY XML FIL ruchilalla J2EE 3 April 29th, 2005 07:19 AM
Adding an Element with Attribute to XML file xergic Classic ASP XML 0 November 20th, 2004 08:26 AM
adding of element and assigning to one element sushovandatta XSLT 2 November 16th, 2004 07:04 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.