将xml文件复制到文本文件中

时间:2012-03-07 16:36:24

标签: c# console-application

我正在尝试将xml文件的内容复制到txt中。但每次我打开txt文件时,它都会显示0.这是我的代码:

using System;
using System.Data;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.XPath;

public class Copy
{
    private static int Main(string[] args)
    {
        DataSet theDataSet = new DataSet();
        theDataSet.ReadXml(@"C:\\Users\mchowdhury\Desktop\MediaInfo.xml"); 
        StreamWriter theWriter = new StreamWriter("test.xml");
        foreach (DataRow curRow in theDataSet.Tables[0].Rows)
        {
            foreach (object curObjectValue in curRow.ItemArray)
            {
                theWriter.Write(curObjectValue);
            }
        }
        theWriter.Close();
    }
}

任何人都可以帮我提出任何建议吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

如果您只是想写出数据并获得样本输出,请尝试以下方法:

DataSet theDataSet = new DataSet();
theDataSet.ReadXml(@"C:\\Users\mchowdhury\Desktop\MediaInfo.xml");
StreamWriter theWriter = new StreamWriter("test.xml");
foreach (DataRow curRow in theDataSet.Tables[0].Rows)
{
    foreach (DataColumn curCol in theDataSet.Tables[0].Columns)
    {
        theWriter.Write(String.Format("{0} ", curRow[curCol]));
    }
}
theWriter.Close();