如何将csv转换为xml,其中csv中的每一列都是通过C#在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>
其中“行代码” =第1列,“ s1” =第2列,“ col code = 5” =第3列,“ col code = 6” =第4列ets。 这就是任务。
现在我有xmlSchema类,csv类,但是在映射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>
我该如何解决?
答案 0 :(得分:0)
这是我的类结构的映射代码
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数据类
// 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数据类
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; }
}