DataSet ds = GetExcelToXml("test.xls");
string filename = @"C:\test.xml";
FileStream myFileStream = new FileStream(filename, FileMode.Create);
XmlTextWriter myXmlWriter = new XmlTextWriter(myFileStream, Encoding.Default);
ds.WriteXml(myXmlWriter);
myXmlWriter.Close();
输出Xml
<NewDataSet>
<Table>
<UserName>bla1</User_Name>
<Mail>bla1@bla2.com</Mail>
<Address>World</Address>
</Table>
</NewDataSet>
我需要Xml节点名称
<ROWS>
<ROW>
<UserName>bla1</User_Name>
<Mail>bla1@bla2.com</Mail>
<Address>World</Address>
</ROW>
</ROWS>
如何制作?
答案 0 :(得分:5)
试试这个,
ds.DataSetName = "ROWS";
ds.Tables[0].TableName = "ROW";
ds.WriteXml(myXmlWriter);
myXmlWriter.Close();
答案 1 :(得分:1)
XmlDocument myXml;
myXml.Load(myXmlWriter); //Not sure if this will work, but you get the idea
myXml.InnerXml = myXml.InnerXml.Replace("< NewDataSet", "< ROWS")
.Replace("< /NewDataSet>", "< /ROWS>")
.Replace("< Table", "< ROW")
.Replace("< /Table>", "< /ROW>");
答案 2 :(得分:0)
这是一个示例C#应用程序,它将读取输入XML,然后使用表名将不同的XML / Data表复制到其他文件:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
DataSet dsXml = new DataSet();
dsXml.ReadXml("mydata.xml");
for (int i = 0; i < dsXml.Tables.Count; i++)
{
Console.WriteLine("Table Name: " + dsXml.Tables[i].TableName);
DataSet newDataSet = new DataSet();
newDataSet.Tables.Add(dsXml.Tables[i].Copy());
FileStream myFileStream = new FileStream(dsXml.Tables[i].TableName + ".xml", FileMode.Create);
XmlTextWriter myXmlWriter = new XmlTextWriter(myFileStream, Encoding.Default);
newDataSet.WriteXml(myXmlWriter);
myXmlWriter.Close();
}
}
}
}
答案 3 :(得分:0)
如果有人来到这里寻找相反方向的问题,其中自写入xml文件以来类型化数据集表名称已更改。
// for xml files created prior to rename of Sample table to SampleS,
// rename the Sample table, read xml,
// then rename table back to current SampleS
if (ds.SampleS.Count == 0)
{
ds = new AnalysisDSX();
ds.Tables["SampleS"].TableName = "Sample";
ds.ReadXml(xmlFilePath);
ds.Tables["Sample"].TableName = "SampleS";
}