Looping through xml and adding items to List<T>

Hi All,

I have an xml result which is returned form a SOLR database which looks similar to :


<doc>
<str name="Name">Tom Maton</str>
<arr name="agent">
    <str>Ted</str>
</arr>
<str name="conformance">extended 1</str>
<str name="Location">London</str>
</doc>
<doc>
<str name="Name">Tom Maton</str>
<arr name="agent">
    <str>Ted</str>
</arr>
<str name="conformance">extended 1</str>
<str name="Location">London</str>
</doc>

So I’m reading it with XMLTextReader and then need to display the Name, Location, agent for each record returned in the xml. I was placing into an ArrayList and passing it back upto the top level.

But recently I’ve been looking to change from array list to List<string> but everytime I loop through the xml I cant seem to add to the List<T>


public class SearchResultsDetails
        {
            public string name{ get; set; }
            public string location { get; set; }
            public string agent { get; set; }
        }

my loop through the xml


var objectDetails = new List<SearchResultsDetails>();

while (solrSearchResults.Read())
            {
                if (solrSearchResults.HasAttributes)
                {

                    if (solrSearchResults.GetAttribute("name") == "identifier")
                        name2 = solrSearchResults.ReadInnerXml().ToString();

                    // need to write functionality to check for multiple titles
                    if (solrSearchResults.GetAttribute("name") == "otherTitle")
                    {
                        location2 = solrSearchResults.ReadInnerXml().ToString();
                    }

                    if (solrSearchResults.GetAttribute("name") == "otherDescription")
                    {
                        agent2 = solrSearchResults.ReadInnerXml().ToString();
                    }

                    objectDetails.Add(new SearchResultsDetails { name = name2 , location = location2, agent = agent2 });
                }
               
            }


But for some reason I cant add found values to items (location, name, agent). I get the error “Use of unassigned local variable ‘title2’”

Any help would be greatly appreciated as its beginning to drive me round the bend.

Thanks

Tom

Hi Tom, something like this should work.


while (solrSearchResults.Read())
		{
			if (solrSearchResults.HasAttributes)
			{
				SearchResultsDetails srd = new SearchResultsDetails();
				if (solrSearchResults.GetAttribute("name") == "identifier")
					srd.name = solrSearchResults.ReadInnerXml().ToString();
				// need to write functionality to check for multiple titles
				if (solrSearchResults.GetAttribute("name") == "otherTitle")
				{
					srd.location = solrSearchResults.ReadInnerXml().ToString();
				}
				if (solrSearchResults.GetAttribute("name") == "otherDescription")
				{
					srd.agent = solrSearchResults.ReadInnerXml().ToString();
				}
				objectDetails.Add(srd);
			}
		}

You could also look into doing it with LinqToXML

Cheers dude, I’ll give that a try.