• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

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

0
rmmarsh
Asked:
rmmarsh
  • 4
  • 3
1 Solution
 
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
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now