使用c#拆分XML文件 [英] XML file split using c#

查看:91
本文介绍了使用c#拆分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.

推荐答案

我认为循环可能是:

I think the loop could be:
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>");
       }


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 ++)//检查没有列

{

行+ = dt.Rows [i] [j] .ToString()+;

if(j%NoOfColumn == 0)//当它是你的最后阶段你没有列

{

Console.WriteLine(行);

lines = names +; <如果(j == dt.Columns.Count - 1 && j%NoOfColumn!= 0)//检查最后一栏

{

Console.WriteLine(行);

}

}

}

}

} 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;我< 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)//当它与你的最后阶段的列数相等时

{

Console.WriteLine(lines);

lines = names +;

}

if(j == lstColumns.Count - 1 && j%NoOfColumn!= 0)//在最后一栏检查

{

Console.WriteLine(lines); < br $>
}

}





}

Console.Read();

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


这篇关于使用c#拆分XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆