So today at school I was given a task to create an application to apply convolution over image. I decided to load convolution cores from xml file. But, as this was the first time me working with xml I have no knowledge how to effective parse it.
So here is what I came up with. I know that there is better solution to this.
Xml sample:
<?xml version="1.0" encoding="utf-8" ?>
<convolution_cores>
<matrix size ="3">
<row index="0">
<column>-1</column>
<column>0</column>
<column>+1</column>
</row>
<row index="1">
<column>-2</column>
<column>0</column>
<column>+2</column>
</row>
<row index="2">
<column>-1</column>
<column>0</column>
<column>+1</column>
</row>
</matrix>
<convolution_cores>
private void LoadFile(string uri, int size)
{
var matrices = XElement.Load(uri);
var matrix = from m in
(from a in matrices.Descendants("matrix")
where (int)a.Attribute("size") == size
select a).Descendants().Descendants()
select m.Value;
// there's got to be a shorter/more efficent way to do the below task
_matrix = new float[size, size];
var str = matrix.GetEnumerator();
str.MoveNext();
for (int i = 0; i < size; i++)
{
for (int j = 0; j < size; j++)
{
_matrix[i, j] = float.Parse(str.Current);
str.MoveNext();
}
}
}