I am reading XML data
and retrieving the values based on the element
. There is one element named <UniqueColumns>
which can have child element called <string>
. I want to read those values and add it to the ObservableCollection<String>
. If there are no values then don't anything. There are three scenarios as following:
Scenario - 1: More than 1 child elements.
<IndexId>4</IndexId>
<UniqueColumns>
<string>Dir_nbr</string>
<string>Dir_name</string>
</UniqueColumns>
<SelectedTableForUniqColumn>TBDirectory</SelectedTableForUniqColumn>
Scenario - 2: Only one child element.
<IndexId>4</IndexId>
<UniqueColumns>
<string>Dir_nbr</string>
</UniqueColumns>
<SelectedTableForUniqColumn>TBDirectory</SelectedTableForUniqColumn>
Scenario - 3: No child element.
<IndexId>4</IndexId>
<UniqueColumns/>
<SelectedTableForUniqColumn>TBDirectory</SelectedTableForUniqColumn>
Code:
//This is a user defined data object and it has parameter which is type of `ObservableCollection<String>`.
ExternalDatabaseTableRequestDO req = new ExternalDatabaseTableRequestDO();
using (XmlTextReader reader = new XmlTextReader(new StringReader(xmlData)))
{
while (reader.Read())
{
int result;
long res;
string parameterValue;
ObservableCollection<String> parameterValueList = new ObservableCollection<String>();
switch (reader.Name.ToLower())
{
case "indexid":
parameterValue = reader.ReadString();
if (!String.IsNullOrWhiteSpace(parameterValue) && Int32.TryParse(parameterValue, out result))
req.IndexId = result;
break;
case "uniquecolumns":
//need loop logic here but not sure how to do that.
if (reader.NodeType == XmlNodeType.Element) // This will give me parent element which is <UniqueColumns>
{
//Stuck here. How to read child elements if exists.
}
break;
case "selectedtableforuniqcolumn":
parameterValue = reader.ReadString();
req.SelectedTableForUniqColumn = parameterValue;
break;
}
}
}
return req;