2010-03-10 4 views
1
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 출력C# 데이터 집합, 노드 이름 바꾸기

<NewDataSet> 
    <Table> 
    <UserName>bla1</User_Name> 
    <Mail>[email protected]</Mail> 
    <Address>World</Address> 
    </Table> 
</NewDataSet> 

내가 XML의 노드 이름이

<ROWS> 
     <ROW> 
     <UserName>bla1</User_Name> 
     <Mail>[email protected]</Mail> 
     <Address>World</Address> 
     </ROW> 
    </ROWS> 

어떻게해야?

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

답변

4

이 시도

, 여기
ds.DataSetName = "ROWS"; 
ds.Tables[0].TableName = "ROW"; 
ds.WriteXml(myXmlWriter); 
myXmlWriter.Close(); 
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>"); 
0

는 테이블 이름을 사용하여 다른 파일을 다른 XML/데이터 테이블을 복사 한 후 입력 XML을 읽고하는 샘플 C# 응용 프로그램입니다
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"; 
}