Click here to Skip to main content
16,011,870 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me.

Coding

C#
void GetXmlValue(int NoOfColumn)
{
    string pathToXmlFile = @"D:\Manas\prodNew.xml";
    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load(pathToXmlFile); // xmldoc.Load(@"D:\Prod.xml");
    XmlNode nodeCol = xmldoc.DocumentElement.FirstChild;
    XmlNode nodeRow = xmldoc.DocumentElement;
    XmlNodeList lstRows = nodeRow.ChildNodes;
    XmlNodeList lstColumns = nodeCol.ChildNodes;


    for (int i = 0; i < lstRows.Count; i++)
    {
        string names = lstRows[i].ChildNodes[0].InnerText + " ";// lstColumns[i].InnerText.ToString();
        if (i > 0) Response.Write("<br/>");
        string lines = names + " ";
        for (int j = 1; j < lstColumns.Count; j++) //Check with no of columns
        {
            lines += lstRows[i].ChildNodes[j].InnerText + " "; //lstColumns[j].InnerText.ToString();
            if (j % NoOfColumn == 0)//When it is equla with your no of columns upto last stage
            {
                Response.Write(lines + "<br/>");
                lines = names + " ";
            }
            if (j == lstColumns.Count - 1 &amp;&amp; j % NoOfColumn != 0) //Check at last column
            {
                Response.Write(lines + "<br/>");
            }
        }
    }
}



Result of output: of GetXmlValue(4)

Mini 2 1957 324 192
Mini 273 837 83 67
Mini 7 40

Standard 1 466 63 140
Standard 35 184 23 51
Standard 72 45

Unknown 98 2734 235 384
Unknown 5223 1261 83 67
Unknown 7 40

Rajesh 90 274 235 384
Rajesh 523 121 73 65
Rajesh 72 40


///////////////////

Now i want create a XML file.like
12 Element like rows. <test>
5 child nodes <c0>mini<c0><c1>2 like....
Posted
Updated 4-Jul-13 23:12pm
v3

C#
void GetXmlValue(int NoOffixedcolumn, int NoOfColumn)
    {
        string pathToXmlFile = @"D:\Manas\prodNew.xml";
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(pathToXmlFile); // xmldoc.Load(@"D:\Prod.xml");

        XmlNode nodeCol = xmldoc.DocumentElement.FirstChild;
        XmlNode nodeRow = xmldoc.DocumentElement;
        XmlNodeList lstRows = nodeRow.ChildNodes;
        XmlNodeList lstColumns = nodeCol.ChildNodes;
        
        using (XmlWriter xmlwriter=XmlWriter.Create(@"D:\Manas\XMLFile.xml"))
        {
            xmlwriter.WriteStartDocument();
            xmlwriter.WriteStartElement("Root");
        for (int i = 0; i < lstRows.Count; i++)
        {
           
          //  if (i > 0) Response.Write("<br />");
            string lines = "", fixColumns = "";
            for (int k = 0; k < NoOffixedcolumn; k++)
            {
                fixColumns += lstRows[i].ChildNodes[k].InnerText + " ";
            }
            lines = fixColumns;
            int counter = 1;
            for (int j = NoOffixedcolumn; j < lstColumns.Count; j++) //Check with no of columns
            {
               
                lines += lstRows[i].ChildNodes[j].InnerText + " ";
                if (counter % NoOfColumn == 0)//When it is equla with your no of columns upto last stage
                {  xmlwriter.WriteStartElement("Test");
                   
                    Response.Write(lines + "<br />");
                    
                    string[] words = lines.Split(' ');


                    for (int l = 0; l < words.Length-1; l++)
                    {
                        xmlwriter.WriteStartElement("C"+l);
                        xmlwriter.WriteValue(words[l]);
                        xmlwriter.WriteEndElement(); // for C
                    }
                   
                    lines = fixColumns ;
                 
                    xmlwriter.WriteEndElement();//for test
                }
                if (j == lstColumns.Count - 1 && counter % NoOfColumn != 0) //Check at last column 
                {
                    xmlwriter.WriteStartElement("Test");
                    string[] words = lines.Split(' ');
                    for (int l = 0; l < words.Length-1; l++)
                    {
                        xmlwriter.WriteStartElement("C" + l);
                        xmlwriter.WriteValue(words[l]);
                        xmlwriter.WriteEndElement(); // for C
                    }
                    Response.Write(lines + "<br />");
                    xmlwriter.WriteEndElement();//for test
                }
                counter += 1;
            }
        }
        xmlwriter.WriteEndElement();
        xmlwriter.WriteEndDocument();
    }
    }
 
Share this answer
 
v2

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