如何将 csv 转换为 xml,其中 csv 中的每一列 = xml 标记的属性
How to convert csv into xml where each column in csv = xml attribute of tag
如何通过 C# 将 csv 转换为 xml,其中 csv 中的每一列都是 xml 标记中的属性?
让我告诉你我的意思:
这是 CSV:
1;A;a;b;c;d;
2;B;e;f;g;h;
这是Xml:
<section>
<row code="1" s1="A">
<col code="5">a</col>
<col code="6">b</col>
<col code="7">c</col>
<col code="8">d</col>
</row>
<row code="2" s1="B">
<col code="5">e</col>
<col code="6">f</col>
<col code="7">g</col>
<col code="8">h</col>
</row>
</section>
其中 "row code" = 第 1 列,"s1"=第 2 列,"col code=5"=第 3 列,"col code=6"=第 4 列等。
这就是任务。
现在我有 xmlSchema class、csv class,但我在映射 csv 时遇到一些问题 - xml。
这是 XMLSchema.cs 的一部分:
public partial class reportSectionsSectionRowCol
{
private string codeField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string code
{ get {return this.codeField;}
set {this.codeField = value;}
}
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get{return this.valueField;}
set{this.valueField = value;}
}
}
col - XML 的元素具有属性代码和值。
<row code="1" s1="A">
<col code="5">a</col>
<col code="6">b</col>
<col code="7">c</col>
<col code="8">d</col>
</row>
这是映射:
static void MappingCSVtoXML(report report, CSVData csvData)
{
if (csvData != null)
{
var col = new reportSectionsSectionRowCol[]{ new reportSectionsSectionRowCol(),
new reportSectionsSectionRowCol(),
new reportSectionsSectionRowCol(),
new reportSectionsSectionRowCol()};
var row = new reportSectionsSectionRow[2] { new reportSectionsSectionRow(), new reportSectionsSectionRow() };
for (int i = 0; i < 2; i++)
{
col[0].code = "5"; col[0].Value = csvData.rows[i].depreciationGroupNumber;
col[1].code = "6"; col[1].Value = csvData.rows[i].commissioningYear;
col[2].code = "7"; col[2].Value = csvData.rows[i].startYear;
col[3].code = "8"; col[3].Value = csvData.rows[i].ageAtLiquidation;
row[i].code = csvData.rows[i].okof.Substring(0, 3); // 1,2
row[i].col = col; // Reference type
row[i].s1 = csvData.rows[i].rowNumber; //A,B
}
report.sections = new reportSectionsSection[1]{new reportSectionsSection(){code = "1", row = row}};
}
}
问题是 col 是引用类型并且只记住最后一个值。
总的结果是
<section>
<row code="1" s1="A">
<col code="5">e</col>
<col code="6">f</col>
<col code="7">g</col>
<col code="8">h</col>
</row>
<row code="2" s1="B">
<col code="5">e</col>
<col code="6">f</col>
<col code="7">g</col>
<col code="8">h</col>
</row>
</section>
我该如何解决?
这是我的class结构
的映射代码
static Section Map( IEnumerable<Csv1Data> source )
{
return new Section
{
Rows = source
.Select( e => new SectionRow
{
Code = e.Code,
Value = e.S1,
Columns = new List<SectionRowCol>
{
new SectionRowCol{ Code = 5, Value = e.Col5, },
new SectionRowCol{ Code = 6, Value = e.Col6, },
new SectionRowCol{ Code = 7, Value = e.Col7, },
new SectionRowCol{ Code = 8, Value = e.Col8, },
}
} )
.ToList(),
};
}
简单的检查和用例
static void Main( string[] args )
{
var source = new List<Csv1Data> {
new Csv1Data { Code = 1, S1 = "A", Col5 = "a", Col6 = "b", Col7 = "c", Col8 = "d" },
new Csv1Data { Code = 2, S1 = "B", Col5 = "a", Col6 = "b", Col7 = "c", Col8 = "d" },
};
var output = Map( source );
XmlSerializer ser = new XmlSerializer( typeof( ExportModels.Xml1.Section ) );
using ( var stream = new MemoryStream() )
{
using ( var writer = new StreamWriter( stream, Encoding.UTF8 ) )
{
ser.Serialize( writer, output );
}
var bytes = stream.ToArray();
var str = Encoding.UTF8.GetString( bytes );
Console.WriteLine( str );
}
}
XML-Data-Classes
// HINWEIS: Für den generierten Code ist möglicherweise mindestens .NET Framework 4.5 oder .NET Core/Standard 2.0 erforderlich.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
[System.Xml.Serialization.XmlRootAttribute( Namespace = "", IsNullable = false, ElementName = "section" )]
public partial class Section
{
private List<SectionRow> _rowField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute( "row" )]
public List<SectionRow> Rows
{
get
{
return this._rowField;
}
set
{
this._rowField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
public partial class SectionRow
{
private List<SectionRowCol> _columnsField;
private int _codeField;
private string _valueField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute( "col" )]
public List<SectionRowCol> Columns
{
get
{
return this._columnsField;
}
set
{
this._columnsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute( AttributeName = "code" )]
public int Code
{
get
{
return this._codeField;
}
set
{
this._codeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute( AttributeName = "s1" )]
public string Value
{
get
{
return this._valueField;
}
set
{
this._valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
public partial class SectionRowCol
{
private byte _codeField;
private string _valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute( AttributeName = "code" )]
public byte Code
{
get
{
return this._codeField;
}
set
{
this._codeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this._valueField;
}
set
{
this._valueField = value;
}
}
}
CSV-Data-Class
class Csv1Data
{
public int Code { get; set; }
public string S1 { get; set; }
public string Col5 { get; set; }
public string Col6 { get; set; }
public string Col7 { get; set; }
public string Col8 { get; set; }
}
如何通过 C# 将 csv 转换为 xml,其中 csv 中的每一列都是 xml 标记中的属性? 让我告诉你我的意思: 这是 CSV:
1;A;a;b;c;d;
2;B;e;f;g;h;
这是Xml:
<section>
<row code="1" s1="A">
<col code="5">a</col>
<col code="6">b</col>
<col code="7">c</col>
<col code="8">d</col>
</row>
<row code="2" s1="B">
<col code="5">e</col>
<col code="6">f</col>
<col code="7">g</col>
<col code="8">h</col>
</row>
</section>
其中 "row code" = 第 1 列,"s1"=第 2 列,"col code=5"=第 3 列,"col code=6"=第 4 列等。 这就是任务。
现在我有 xmlSchema class、csv class,但我在映射 csv 时遇到一些问题 - xml。 这是 XMLSchema.cs 的一部分:
public partial class reportSectionsSectionRowCol
{
private string codeField;
private string valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string code
{ get {return this.codeField;}
set {this.codeField = value;}
}
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get{return this.valueField;}
set{this.valueField = value;}
}
}
col - XML 的元素具有属性代码和值。
<row code="1" s1="A">
<col code="5">a</col>
<col code="6">b</col>
<col code="7">c</col>
<col code="8">d</col>
</row>
这是映射:
static void MappingCSVtoXML(report report, CSVData csvData)
{
if (csvData != null)
{
var col = new reportSectionsSectionRowCol[]{ new reportSectionsSectionRowCol(),
new reportSectionsSectionRowCol(),
new reportSectionsSectionRowCol(),
new reportSectionsSectionRowCol()};
var row = new reportSectionsSectionRow[2] { new reportSectionsSectionRow(), new reportSectionsSectionRow() };
for (int i = 0; i < 2; i++)
{
col[0].code = "5"; col[0].Value = csvData.rows[i].depreciationGroupNumber;
col[1].code = "6"; col[1].Value = csvData.rows[i].commissioningYear;
col[2].code = "7"; col[2].Value = csvData.rows[i].startYear;
col[3].code = "8"; col[3].Value = csvData.rows[i].ageAtLiquidation;
row[i].code = csvData.rows[i].okof.Substring(0, 3); // 1,2
row[i].col = col; // Reference type
row[i].s1 = csvData.rows[i].rowNumber; //A,B
}
report.sections = new reportSectionsSection[1]{new reportSectionsSection(){code = "1", row = row}};
}
}
问题是 col 是引用类型并且只记住最后一个值。 总的结果是
<section>
<row code="1" s1="A">
<col code="5">e</col>
<col code="6">f</col>
<col code="7">g</col>
<col code="8">h</col>
</row>
<row code="2" s1="B">
<col code="5">e</col>
<col code="6">f</col>
<col code="7">g</col>
<col code="8">h</col>
</row>
</section>
我该如何解决?
这是我的class结构
的映射代码static Section Map( IEnumerable<Csv1Data> source )
{
return new Section
{
Rows = source
.Select( e => new SectionRow
{
Code = e.Code,
Value = e.S1,
Columns = new List<SectionRowCol>
{
new SectionRowCol{ Code = 5, Value = e.Col5, },
new SectionRowCol{ Code = 6, Value = e.Col6, },
new SectionRowCol{ Code = 7, Value = e.Col7, },
new SectionRowCol{ Code = 8, Value = e.Col8, },
}
} )
.ToList(),
};
}
简单的检查和用例
static void Main( string[] args )
{
var source = new List<Csv1Data> {
new Csv1Data { Code = 1, S1 = "A", Col5 = "a", Col6 = "b", Col7 = "c", Col8 = "d" },
new Csv1Data { Code = 2, S1 = "B", Col5 = "a", Col6 = "b", Col7 = "c", Col8 = "d" },
};
var output = Map( source );
XmlSerializer ser = new XmlSerializer( typeof( ExportModels.Xml1.Section ) );
using ( var stream = new MemoryStream() )
{
using ( var writer = new StreamWriter( stream, Encoding.UTF8 ) )
{
ser.Serialize( writer, output );
}
var bytes = stream.ToArray();
var str = Encoding.UTF8.GetString( bytes );
Console.WriteLine( str );
}
}
XML-Data-Classes
// HINWEIS: Für den generierten Code ist möglicherweise mindestens .NET Framework 4.5 oder .NET Core/Standard 2.0 erforderlich.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
[System.Xml.Serialization.XmlRootAttribute( Namespace = "", IsNullable = false, ElementName = "section" )]
public partial class Section
{
private List<SectionRow> _rowField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute( "row" )]
public List<SectionRow> Rows
{
get
{
return this._rowField;
}
set
{
this._rowField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
public partial class SectionRow
{
private List<SectionRowCol> _columnsField;
private int _codeField;
private string _valueField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute( "col" )]
public List<SectionRowCol> Columns
{
get
{
return this._columnsField;
}
set
{
this._columnsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute( AttributeName = "code" )]
public int Code
{
get
{
return this._codeField;
}
set
{
this._codeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute( AttributeName = "s1" )]
public string Value
{
get
{
return this._valueField;
}
set
{
this._valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute( "code" )]
[System.Xml.Serialization.XmlTypeAttribute( AnonymousType = true )]
public partial class SectionRowCol
{
private byte _codeField;
private string _valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute( AttributeName = "code" )]
public byte Code
{
get
{
return this._codeField;
}
set
{
this._codeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public string Value
{
get
{
return this._valueField;
}
set
{
this._valueField = value;
}
}
}
CSV-Data-Class
class Csv1Data
{
public int Code { get; set; }
public string S1 { get; set; }
public string Col5 { get; set; }
public string Col6 { get; set; }
public string Col7 { get; set; }
public string Col8 { get; set; }
}