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;

2 Answers 2

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();
Sign up to request clarification or add additional context in comments.

4 Comments

for some reason I'm getting error 'System.Xml.Linq.XDocument' does not contain a definition for 'XPathSelectElements'.
Nevermind I fixed it but getting another error Illegal characters in path.
@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.
thanks for your suggestions. I've posted my approach below.
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);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.