Hi all,
I have a question is it possible to write an XML file directly from a two dimensional array? My array looks like this:
Id False
Chapter False
Count True
Materialcosts True
Materialcosts_Total False
Norm False
Hours False
Labourcost False
Labourcost_Total False
Materialcode False
Total False
The name of the array is ColumnsArray, an the resulting XML file should like this:
<?xml version="1.0" encoding="utf-8"?>
< !--XML Database.-->
< Data>
<DGV>
<Column Name="Id" Visible="False"></Column>
<Column Name="Capter" Visible="False"></Column>
<Column Name="Count" Visible="True"></Column>
<Column Name="Materialcosts" Visible="True"></Column>
<Column Name="Materialcosts_total" Visible="False"></Column>
<Column Name="Norm" Visible="False"></Column>
<Column Name="Hours" Visible="False"></Column>
<Column Name="Labourcost" Visible="False"></Column>
<Column Name="Labourcost_Total" Visible="False"></Column>
<Column Name="Materialcode" Visible="False"></Column>
<Column Name="Total" Visible="False"></Column>
</DGV>
< /Data>
A couple of weeks ago I got some code from Karen Payne, to write a List to XML, this is the code:
'Create the list and read the data from an XML file
Dim ColumnInfoList As List(Of ColumnInformation)=
(
From item In XDocument.Load(IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"MyData.xml"))...<Column>
SelectNew ColumnInformation With
{
.Name = item.@Name,
.Visible =CBool(item.@Visible)
}
).ToList
Dim DataXml =
<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
<Data>
<DGV>
<%= From cIn ColumnsArray Select
<Column name=<%= ColumnsArray(c,0)%> visible=<%= ColumnsArray(c,1)%>>
</Column>%>
</DGV>
</Data>
Dim sb AsNew System.Text.StringBuilder
sb.AppendLine("<?xml version='1.0' encoding='utf-8'?>")
sb.AppendLine(DataXml.ToString)
IO.File.WriteAllText(IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"test.xml"), sb.ToString)
Class ColumnInformation
Property Name AsString
Property Visible AsBoolean
EndClass
So in the DataXML variable, the data from the ColumnInfoList is written. I tried to replace the ColumninfoList with the name of my array, like this:
Dim DataXml =
<?xml version="1.0" encoding="utf-8"?>
<!--XML Database.-->
<Data>
<DGV>
<%= From cIn ColumnInfoList Select
<Column name=<%= c.Name%> visible=<%= c.Visible%>>
</Column>%>
</DGV>
</Data>
But then I got an error:
Additional information: Conversion from string "Id" to type 'Integer' is not valid.
I don't know what I'm doing wrong.
Any Hints?
Regards,
Mark