2012-10-25 12 views
6

DataTable In C# of following formatZałóż XML z DataTable

C#: chcę przekonwertować tę tabelę w formacie XML. Proszę zignorować błędy w nazwach wierszy. To są dane testowe. Podałem próbkę dwóch kolumn przekonwertowanych do formatu xml, a odpowiednie wiersze jako atrybuty. Ale ja naprawdę chcę dla wszystkich kolumn. To jest DataCable.

<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

* w drugim xelementie. To był błąd literowy. – StackOverflowVeryHelpful

+0

użyć DataTable.WriteXml http://msdn.microsoft.com/en-us/library/system.data.datatable.writexml.aspx –

Odpowiedz

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

Ten pracował dla mnie świetnie. Dzięki Stackoverflow.

3

DataTable s przeznaczone są iteracyjne nad wiersze, a nie kolumny jak trzeba. Nie ma nic wbudowanego, aby utrzymać DataTable w kolumnie głównej. Będziesz używał niestandardowego kodu. Pseudo-kod byłoby coś jak:

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

Muszę pominąć pierwszą kolumnę. Gdyby ktoś mógł mi podać kod, byłoby świetnie – StackOverflowVeryHelpful

+0

Dodałem, że pominąłem kolumnę "Nazwa", ale będziesz musiał sam wykonać trochę pracy. Jeśli masz konkretne pytania, możesz je opublikować jako nowe pytania, ale wątpię, że masz zamiar poprosić kogoś o napisanie wszystkiego dla ciebie. –