2012-10-25 69 views
6

DataTable In C# of following formatTạo XML từ một DataTable

Sử dụng C#: Tôi muốn chuyển đổi bảng này thành XML. Vui lòng bỏ qua những sai lầm trong tên hàng. Đây là dữ liệu thử nghiệm. Tôi đã đưa ra mẫu của hai cột được chuyển đổi thành xml và các hàng tương ứng làm thuộc tính. Nhưng tôi thực sự muốn cho tất cả các cột. Đây là một Datatable.

<ListDataCollateralDials> 
           <DataCollateralDials Type="Conv"> 
            <Multiplier>1</Multiplier> 
            <Seasoning>1</Seasoning> 
            <Lockin>1</Lockin> 
            <Multiplier>1</Multiplier> 
            <ElbowShift>0</ElbowShift> 
            <Steepness>1</Steepness> 
            <Burnout>1</Burnout> 
            <Adjustment >1</Adjustment> 
            <Effect>1</Effect> 
            <Decay>1</Decay> 
            <Outs>1</Outs> 
            <Base>700</Base> 
            <Slope>1</Slope> 
            <Base>80</Base> 
            <Slope2>1</Slope2> 
            <Base2>200</Base2> 
            <Slope3>1</Slope3> 
            <Height>0</Height> 
            <Length>0</Length> 
            <Height2>0</Height2> 
            <Length2>0</Length2> 
            <Elbow>0</Elbow> 
               <Multiplier2>1</Multiplier2> 
            <Multiplier3>1</Multiplier3> 

           </DataCollateralDials> 
<DataCollateralDials Type="Conv"> 
           <Multiplier>1</Multiplier> 
           <Seasoning>1</Seasoning> 
           <Lockin>1</Lockin> 
           <Multiplier>1</Multiplier> 
           <ElbowShift>0</ElbowShift> 
           <Steepness>1</Steepness> 
           <Burnout>1</Burnout> 
           <Adjustment >1</Adjustment> 
           <Effect>1</Effect> 
           <Decay>1</Decay> 
           <Outs>1</Outs> 
           <Base>700</Base> 
           <Slope>1</Slope> 
           <Base>80</Base> 
           <Slope2>1</Slope2> 
           <Base2>200</Base2> 
           <Slope3>1</Slope3> 
           <Height>0</Height> 
           <Length>0</Length> 
           <Height2>0</Height2> 
           <Length2>0</Length2> 
           <Elbow>0</Elbow> 
           <Multiplier2>1</Multiplier2> 
           <Multiplier3>1</Multiplier3> 

          </DataCollateralDials> 
</ListDataCollateralDials> 
+0

* trong xelement thứ hai. Đây là lỗi chính tả – StackOverflowVeryHelpful

+0

sử dụng DataTable.WriteXml http://msdn.microsoft.com/en-us/library/system.data.datatable.writexml.aspx –

Trả lời

4
public static string ToXml(this DataTable table, int metaIndex = 0) 
{ 
    XDocument xdoc = new XDocument(
     new XElement(table.TableName, 
      from column in table.Columns.Cast<DataColumn>() 
      where column != table.Columns[metaIndex] 
      select new XElement(column.ColumnName, 
       from row in table.AsEnumerable() 
       select new XElement(row.Field<string>(metaIndex), row[column]) 
       ) 
      ) 
     ); 

    return xdoc.ToString(); 
} 

này làm việc rất lớn đối với tôi. Cảm ơn stackoverflow.

3

DataTable s được thiết kế để lặp qua hàng, chứ không phải cột như bạn có. Không có gì được tích hợp để duy trì một số DataTable theo thứ tự cột lớn. Bạn sẽ sử dụng mã tùy chỉnh. Pseudo-code sẽ là một cái gì đó như:

foeeach(DataColumn) 
    if(name != "Name") 
    output column header 
    foreach(DataRow) 
     output row value 
+0

Tôi phải bỏ qua cột đầu tiên. Nếu ai đó có thể cung cấp cho tôi mã, nó sẽ là tuyệt vời – StackOverflowVeryHelpful

+0

Tôi cũng đã bỏ qua cột "Tên" nhưng bạn sẽ phải tự mình làm một số công việc. Nếu bạn có câu hỏi cụ thể, bạn có thể đăng chúng dưới dạng câu hỏi mới tại đây nhưng tôi nghi ngờ bạn sẽ có người viết toàn bộ nội dung cho bạn. –

Các vấn đề liên quan