[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Problem reading XML

Posted on 2008-11-12
7
Medium Priority
?
330 Views
Last Modified: 2012-05-05
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
Comment
Question by:rmmarsh
  • 4
  • 3
7 Comments
 
LVL 12

Expert Comment

by:jandromeda
ID: 22946514
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
 

Author Comment

by:rmmarsh
ID: 22949529
Compile error:  "childNode" does not exist
0
 
LVL 12

Expert Comment

by:jandromeda
ID: 22950245
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:rmmarsh
ID: 22951494
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
 
LVL 12

Expert Comment

by:jandromeda
ID: 22951762
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
 

Author Comment

by:rmmarsh
ID: 22952173
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
 

Accepted Solution

by:
rmmarsh earned 0 total points
ID: 22996992
I figured it out myself... one of the fields (title) was too long.  Thanks for the replies tho'
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question