Problem reading XML

I have the XML file below... I have the code reading the XML file also... I can read the first 4 items, then it dies on the 5th with "Object reference not set to an instance of an object." referring to the Author line (as indicated on the code)

What is causing this?
foreach (XmlNode item in bookInfo)
                {
                    sData sD = new sData();  //  structure - one for each ASIN listed
 
                    sD.ASIN = doc.GetElementsByTagName("ASIN")[i].InnerText.ToString();
                    sD.Author = doc.GetElementsByTagName("Author")[i].InnerText.ToString();
                    sD.Publisher = doc.GetElementsByTagName("Manufacturer")[i].InnerText.ToString();
                    sD.Title = doc.GetElementsByTagName("Title")[i].InnerText.ToString();
                    //sD.Comments = getComments(doc.GetElementsByTagName("DetailPageURL")[i].InnerText.ToString());
                    i++;
                    alData.Add(sD);  //  add this to the structure array
 
-------------------------------    XML file follows    ----------------------
 
  <?xml version="1.0" ?> 
- <ItemSearchResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2005-10-05">
- <OperationRequest>
  <RequestId>28726e69-7ba7-4157-a315-d98ac7b97683</RequestId> 
- <Arguments>
  <Argument Name="Sort" Value="salesrank" /> 
  <Argument Name="Operation" Value="ItemSearch" /> 
  <Argument Name="Service" Value="AWSECommerceService" /> 
  <Argument Name="Power" Value="title:Rapture, author:Ron Raffaelli, publisher:Grove Press" /> 
  <Argument Name="SubscriptionId" Value="---------------" /> 
  <Argument Name="SearchIndex" Value="Books" /> 
  </Arguments>
  <RequestProcessingTime>0.0498870000000000</RequestProcessingTime> 
  </OperationRequest>
- <Items>
- <Request>
  <IsValid>True</IsValid> 
- <ItemSearchRequest>
  <Condition>New</Condition> 
  <DeliveryMethod>Ship</DeliveryMethod> 
  <MerchantId>Amazon</MerchantId> 
  <Power>title:Rapture, author:Ron Raffaelli, publisher:Grove Press</Power> 
  <ResponseGroup>Small</ResponseGroup> 
  <SearchIndex>Books</SearchIndex> 
  <Sort>salesrank</Sort> 
  </ItemSearchRequest>
  </Request>
  <TotalResults>5</TotalResults> 
  <TotalPages>1</TotalPages> 
- <Item>
  <ASIN>B0006WMWNA</ASIN> 
  <DetailPageURL>http://www.amazon.com/Rapture-erotic-fantasies-Ron-Raffaelli/dp/B0006WMWNA%3FSubscriptionId%3D 084BT0EPNB27A07DGR82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0006WMWNA</DetailPageURL> 
- <ItemAttributes>
  <Author>Ron Raffaelli</Author> 
  <Manufacturer>Grove Press</Manufacturer> 
  <ProductGroup>Book</ProductGroup> 
  <Title>Rapture: 13 erotic fantasies</Title> 
  </ItemAttributes>
  </Item>
- <Item>
  <ASIN>B000GVMNZ8</ASIN> 
  <DetailPageURL>http://www.amazon.com/Rapture-Erotic-Fantasies-Ron-Raffaelli/dp/B000GVMNZ8%3FSubscriptionId%3D 084BT0EPNB27A07DGR82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB000GVMNZ8</DetailPageURL> 
- <ItemAttributes>
  <Creator Role="Photographer">Ron Raffaelli</Creator> 
  <Manufacturer>Grove Press, NY</Manufacturer> 
  <ProductGroup>Book</ProductGroup> 
  <Title>Rapture: 13 Erotic Fantasies</Title> 
  </ItemAttributes>
  </Item>
- <Item>
  <ASIN>B0010VBDZ4</ASIN> 
  <DetailPageURL>http://www.amazon.com/Rapture-13-Erotic-Fantasies-Ron-Raffaelli/dp/B0010VBDZ4%3FSubscriptionId%3D 084BT0EPNB27A07DGR82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB0010VBDZ4</DetailPageURL> 
- <ItemAttributes>
  <Author>Ron Raffaelli</Author> 
  <Creator Role="Illustrator">Ron Raffaelli Photos</Creator> 
  <Manufacturer>Grove Press</Manufacturer> 
  <ProductGroup>Book</ProductGroup> 
  <Title>Rapture-13 Erotic Fantasies</Title> 
  </ItemAttributes>
  </Item>
- <Item>
  <ASIN>B000IZHYEW</ASIN> 
  <DetailPageURL>http://www.amazon.com/RAPTURE-Erotic-Fantasies-Photographed-Raffaelli/dp/B000IZHYEW%3FSubscriptionId%3D 084BT0EPNB27A07DGR82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB000IZHYEW</DetailPageURL> 
- <ItemAttributes>
  <Author>Ron RAFFAELLI</Author> 
  <Manufacturer>Media Press (Grove)</Manufacturer> 
  <ProductGroup>Book</ProductGroup> 
  <Title>RAPTURE: 13 Erotic Fantasies Photographed By Raffaelli</Title> 
  </ItemAttributes>
  </Item>
- <Item>
  <ASIN>B000ZC7WQI</ASIN> 
  <DetailPageURL>http://www.amazon.com/Rapture-13-Erotic-Fantasies-Photographed/dp/B000ZC7WQI%3FSubscriptionId%3D 084BT0EPNB27A07DGR82%26tag%3Dws%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB000ZC7WQI</DetailPageURL> 
- <ItemAttributes>
  <Author>Ron Raffaelli</Author> 
  <Manufacturer>GROVE PRESS * (NY)</Manufacturer> 
  <ProductGroup>Book</ProductGroup> 
  <Title>Rapture 13 Erotic Fantasies Photographed</Title> 
  </ItemAttributes>
  </Item>
  </Items>
  </ItemSearchResponse>
                }

Open in new window

rmmarshAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jandromedaCommented:
Assuming that you are iterating through "Item" nodes I have written this code. Try it and let me know.
foreach (XmlNode node in list)
			{
				childNode = node.SelectSingleNode("ASIN");
				
				if (childNode != null)
				{
					Console.WriteLine(string.Concat(childNode.Name, ": ", childNode.InnerText));
				}
				                  
				childNode = node.SelectSingleNode("ItemAttributes/Author");
				
				if (childNode != null)
				{
					Console.WriteLine(string.Concat(childNode.Name, ": ", childNode.InnerText));
				}
				
				childNode = node.SelectSingleNode("ItemAttributes/Manufacturer");
				
				if (childNode != null)
				{
					Console.WriteLine(string.Concat(childNode.Name, ": ", childNode.InnerText));
				}
				
				childNode = node.SelectSingleNode("ItemAttributes/Title");
				
				if (childNode != null)
				{
					Console.WriteLine(string.Concat(childNode.Name, ": ", childNode.InnerText));
				}
			}

Open in new window

0
rmmarshAuthor Commented:
Compile error:  "childNode" does not exist
0
jandromedaCommented:
Oh I'm sorry. I showed you that code hoping that you can understand how it has to be done and modified the your code as necessary. Anyway here is the code with the "childNode" declaration.

			XmlNode childNode = null;
			
			foreach (XmlNode node in list)
			{
				childNode = node.SelectSingleNode("ASIN");
				
				if (childNode != null)
				{
					Console.WriteLine(string.Concat(childNode.Name, ": ", childNode.InnerText));
				}
				                  
				childNode = node.SelectSingleNode("ItemAttributes/Author");
				
				if (childNode != null)
				{
					Console.WriteLine(string.Concat(childNode.Name, ": ", childNode.InnerText));
				}
				
				childNode = node.SelectSingleNode("ItemAttributes/Manufacturer");
				
				if (childNode != null)
				{
					Console.WriteLine(string.Concat(childNode.Name, ": ", childNode.InnerText));
				}
				
				childNode = node.SelectSingleNode("ItemAttributes/Title");
				
				if (childNode != null)
				{
					Console.WriteLine(string.Concat(childNode.Name, ": ", childNode.InnerText));
				}
			}
		}

Open in new window

0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

rmmarshAuthor Commented:
Tried that and got nothing in the Output window using the debugger.  The problem is not that there is a missing field, but it thinks there is... Something in my code is wrong; it doesn't happen all of the time, only sporadically...

0
jandromedaCommented:
Ok then post your code. I'll have a look at it. But I can make sure that the program read the xml values correctly or not.
0
rmmarshAuthor Commented:
Use the data I attached in the original question...
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(" http://xml.amazon.com/onca/xml?Service=AWSECommerceService" +
                      "&Operation=ItemSearch" +
                      "&SubscriptionId=  " + awsKey +      //  084BT0EPNB27A07DGR82
                      "&SearchIndex=Books" +
                       "&Power=title:" + title + ", author:" + author + ", publisher:" + pub +
                       "&Sort=salesrank");
 
                string x = doc.InnerXml;
 
                //  check for errors
                XmlNodeList error = doc.GetElementsByTagName("Error");
 
                foreach (XmlNode node in error)
                {
                    XmlElement bookElement = (XmlElement)node;
                    string errorMsg = bookElement.GetElementsByTagName("Message")[0].InnerText;
 
                    //  We did not find any matches for your request.
 
                    if (errorMsg.Contains("is not a valid value for ItemId"))
                        return false;
                    else
                    {
                        MessageBox.Show("AWS Error: " + errorMsg, "Prager, Software", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return false;
                    }
                }
 
 
                XmlNodeList bookInfo = doc.GetElementsByTagName("Item");
                int i = 0;
 
                foreach (XmlNode item in bookInfo)
                {
                    sData sD = new sData();  //  structure - one for each ASIN listed
 
                    sD.ASIN = doc.GetElementsByTagName("ASIN")[i].InnerText.ToString();
                    sD.Author = doc.GetElementsByTagName("Author")[i].InnerText.ToString();
                    sD.Publisher = doc.GetElementsByTagName("Manufacturer")[i].InnerText.ToString();
                    sD.Title = doc.GetElementsByTagName("Title")[i].InnerText.ToString();
                    //sD.Comments = getComments(doc.GetElementsByTagName("DetailPageURL")[i].InnerText.ToString());
                    i++;
                    alData.Add(sD);  //  add this to the structure array
                }

Open in new window

0
rmmarshAuthor Commented:
I figured it out myself... one of the fields (title) was too long.  Thanks for the replies tho'
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.