Monday, November 19, 2007

Missing data in XML (format FMPXMLRESULT)

No comments:
We usually have XML data with some value missing, like in this example:



<COL>
<DATA>Value1</DATA>
<DATA>Value2</DATA>
<DATA>Value3</DATA>
<DATA>Value4</DATA>
</COL>
<COL>
<DATA>240</DATA>
<DATA />
<DATA>6</DATA>
<DATA>6</DATA>
</COL>



In order that the XML reader overlooks the data missing at the second position in a consistent way, the following condition can be used (the following code is "conceptual", which means that it is not the real one that is currently running):


(...)

if (currentReader.NodeType == XmlNodeType.Text)
{
if (isRowToRetrieve)
{
rowInfo[columnIndex] = reader.Value;

if (columnIndex == ProdIDPos)
{
//Values capture
valuesArray[valuesIndex++] = currentReader.Value;
}
else if (columnIndex == QuantityPos)
{
//Quantities capture
quantitiesArray[quantitiesIndex++] = currentReader.Value;
}
else if{(...)}
else{(...)}
}
}

(...)

if ((lastElementName == "DATA") && (currentReader.Name == "DATA") && (lastNodeType != XmlNodeType.Text))
{
Log("Quantity for "+ valuesArray[quantitiesIndex] +" is missing");
}



Then the key item here is the consideration that, between a XML ElementName and EndElementName, a XML NodeType Text is expected