0

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;
GThree
  • 2,708
  • 7
  • 34
  • 67

2 Answers2

1

How about using Linq2Xml?

var xDoc = XDocument.Load(filename);
//var xDoc = XDocument.Parse(xmlstring);
var strings = xDoc.XPathSelectElements("//UniqueColumns/string")
                .Select(x => x.Value)
                .ToList();
EZI
  • 15,209
  • 2
  • 27
  • 33
  • for some reason I'm getting error `'System.Xml.Linq.XDocument' does not contain a definition for 'XPathSelectElements'`. – GThree Jul 15 '15 at 18:24
  • Nevermind I fixed it but getting another error `Illegal characters in path.` – GThree Jul 15 '15 at 18:30
  • @Robinhood If you give an *xml string* to XDocument use `XDocument.Parse` method. `XDocument.Load` loads the xml from a file. I updated the answer. – EZI Jul 15 '15 at 18:38
  • thanks for your suggestions. I've posted my approach below. – GThree Jul 15 '15 at 18:49
0
//This will give me all child element values if they exists   
 var columnList = XElement.Parse(xmlData).Descendants("string").ToList();

        if (columnList != null)
        {
            foreach (var column in columnList)
                parameterValueList.Add(column.Value);
        }
GThree
  • 2,708
  • 7
  • 34
  • 67