Click here to Skip to main content
15,919,434 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
XmlDocument xmlDoc = new XmlDocument();
       xmlDoc.Load(@"D:\Prod.xml");

      XmlNode node = xmlDoc.DocumentElement.FirstChild;
      XmlNodeList lstVideos = node.ChildNodes;
      for (int i = 0; i < lstVideos.Count; i++)
       {

           for (int j = 0; j < 3; j++)
           {
               Response.Write("  " + lstVideos[j].InnerText + " ");
           }
           Response.Write("<br>");
       }





xml file is:
 Collapse | Copy Code
<?xml version="1.0"?>
-<DocumentElement>
-<test>
<c0>manas</c0>
<c1>100</c1>
<c2>20</c2>
<c3>33</c3>
<c4>54</c4>
 <c5>65</c5>
<c6>78</c6>
</test>


So i want
like
manas 100 20
manas 33 54
manas 65 78

but coming like
manas 100 20
manas 100 20
manas 100 20
manas 100 20
manas 100 20

Note:
Column numbers are not fixed like 7.
it may be 200, or 100.


plz help me.
Posted

I think the loop could be:
C#
String firstItem = lstVideos[0].InnerText;
for (int i = 1; i < lstVideos.Count; i+=2)
       {
           Response.Write(firstItem + " " + lstVideos[i].InnerText + " " + lstVideos[i+1] + " ");
           Response.Write("<br>");
       }
 
Share this answer
 
static void GetXMLDocument(int NoOfColumn)
{
string pathToXmlFile = @"D:\Manas\Prod.xml";
DataTable dt = new DataTable();
DataSet ds = new DataSet();
ds.ReadXml(pathToXmlFile);
if (ds.Tables.Count > 0)
{
dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
string names = dt.Rows[i][0].ToString();
if (i > 0) Console.WriteLine(" ");
string lines = names + " ";
for (int j = 1; j < dt.Columns.Count; j++) //Check with no of columns
{
lines += dt.Rows[i][j].ToString() + " ";
if (j % NoOfColumn == 0)//When it is equla with your no of columns upto last stage
{
Console.WriteLine(lines);
lines = names + " ";
}
if (j == dt.Columns.Count - 1 && j % NoOfColumn != 0) //Check at last column
{
Console.WriteLine(lines);
}
}
}
}
} Console.Read();
}


public static void GetXMLValue(int NoOfColumn)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(@"D:\Manas\ProdNew.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].InnerText.ToString();
if (i > 0) Console.WriteLine("");
string lines = names + " ";
for (int j = 1; j {
lines += lstRows[i].InnerText.ToString() + " ";
if (j % NoOfColumn == 0)//When it is equla with your no of columns upto last stage
{
Console.WriteLine(lines);
lines = names + " ";
}
if (j == lstColumns.Count - 1 && j % NoOfColumn != 0) //Check at last column
{
Console.WriteLine(lines);
}
}


}
Console.Read();
}
 
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