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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

XmlTextReader: how to read entire file

I am having problems with my XMLTextReader... (see code below)... I keep getting a "root element not found".  Is there a way I can see where I am, like at the first read?  (XMLBookmarkReader can be found here:  http://msdn.microsoft.com/en-us/library/aa302292.aspx)

 
// Create an insntance of XmlTextReader and call Read method to read the file
            XmlTextReader tr = new XmlTextReader(sr);
            tr.WhitespaceHandling = WhitespaceHandling.None;
            XmlBookmarkReader br = new XmlBookmarkReader(tr);

            bool notFound = true;

            try {
                br.Read();
                br.SetBookmark("start");

                string errorMsg = "";
                br.MoveToAttribute("Errors");
                while (br.Read()) {
                    if (br.NodeType == XmlNodeType.Element && br.NodeType != XmlNodeType.EndElement)
                        switch (br.LocalName) {
                            case "Message":
                                br.Read();  //  get value
                                errorMsg = br.Value.Length > 50 ? br.Value.Substring(0, 50) : br.Value;
                                if (errorMsg.Length > 0)
                                    return false;
                                break;
                        }
                }

            }
            catch (Exception ex) {
                if (ex.Message.Contains("Root element is missing")) {
                    MessageBox.Show("Data returned from Amazon.com is invalid (Root element missing)", "Prager, Software", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                else {
                    MessageBox.Show("InvokeItemSearch - Unknown error: " + ex.Message, "Prager, Software", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    throw new System.ArgumentException("Exception-833");
                }
            }

            br.ReturnToAndRemoveBookmark("start");  //  return to beginning
            br.Read();
            br.MoveToAttribute("ItemAttributes");

Open in new window

0
rmmarsh
Asked:
rmmarsh
  • 8
  • 5
1 Solution
 
käµfm³d 👽Commented:
Do you have a sample of XML?
0
 
rmmarshAuthor Commented:
Here you go...

<?xml version="1.0" encoding="UTF-8"?><ItemLookupResponse xmlns="http://webservices.amazon.com/AWSECommerceService/2011-08-01"><OperationRequest><HTTPHeaders><Header Name="UserAgent" Value="Jakarta Commons-HttpClient/3.0.1"></Header></HTTPHeaders><RequestId>0WHVK4S3PMADD732JTM6</RequestId><Arguments><Argument Name="SearchIndex" Value="Books"></Argument><Argument Name="MerchantId" Value="All"></Argument><Argument Name="AssociateTag" Value="pragbook-20"></Argument><Argument Name="ItemId" Value="0155719653"></Argument><Argument Name="Service" Value="AWSECommerceService"></Argument><Argument Name="Signature" Value="n7IcxN5wq5qKD7CWaLF09RdLaFb876fmQ1Fe0wbRGyg="></Argument><Argument Name="ResponseGroup" Value="Medium"></Argument><Argument Name="Operation" Value="ItemLookup"></Argument><Argument Name="IdType" Value="ISBN"></Argument><Argument Name="AWSAccessKeyId" Value="084BT0EPNB27A07DGR82"></Argument><Argument Name="Timestamp" Value="2011-10-22T14:14:20Z"></Argument><Argument Name="Version" Value="2011-08-01"></Argument></Arguments><RequestProcessingTime>0.0654439926147461</RequestProcessingTime></OperationRequest><Items><Request><IsValid>True</IsValid><ItemLookupRequest><IdType>ISBN</IdType><MerchantId>All</MerchantId><ItemId>0155719653</ItemId><ResponseGroup>Medium</ResponseGroup><SearchIndex>Books</SearchIndex></ItemLookupRequest></Request><Item><ASIN>B004B6LEPY</ASIN><DetailPageURL>http://www.amazon.com/Private-American-History-everyday-paperback/dp/B004B6LEPY%3FSubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3DB004B6LEPY</DetailPageURL><ItemLinks><ItemLink><Description>Technical Details</Description><URL>http://www.amazon.com/Private-American-History-everyday-paperback/dp/tech-data/B004B6LEPY%3FSubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004B6LEPY</URL></ItemLink><ItemLink><Description>Add To Baby Registry</Description><URL>http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3DB004B6LEPY%26SubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004B6LEPY</URL></ItemLink><ItemLink><Description>Add To Wedding Registry</Description><URL>http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3DB004B6LEPY%26SubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004B6LEPY</URL></ItemLink><ItemLink><Description>Add To Wishlist</Description><URL>http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3DB004B6LEPY%26SubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004B6LEPY</URL></ItemLink><ItemLink><Description>Tell A Friend</Description><URL>http://www.amazon.com/gp/pdp/taf/B004B6LEPY%3FSubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004B6LEPY</URL></ItemLink><ItemLink><Description>All Customer Reviews</Description><URL>http://www.amazon.com/review/product/B004B6LEPY%3FSubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004B6LEPY</URL></ItemLink><ItemLink><Description>All Offers</Description><URL>http://www.amazon.com/gp/offer-listing/B004B6LEPY%3FSubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3DB004B6LEPY</URL></ItemLink></ItemLinks><ItemAttributes><Author>edited by Thomas Frazier</Author><Binding>Unknown Binding</Binding><EAN>9780155719651</EAN><EANList><EANListElement>9780155719651</EANListElement></EANList><ISBN>0155719653</ISBN><Label>Harcourt Brace Jovanovich</Label><Manufacturer>Harcourt Brace Jovanovich</Manufacturer><ProductGroup>Book</ProductGroup><ProductTypeName>BOOKS_1973_AND_LATER</ProductTypeName><PublicationDate>1979</PublicationDate><Publisher>Harcourt Brace Jovanovich</Publisher><Studio>Harcourt Brace Jovanovich</Studio><Title>The Private Side of American History second edition (2nd edition) reading in everyday life 1979 paperback</Title></ItemAttributes><OfferSummary><LowestUsedPrice><Amount>400</Amount><CurrencyCode>USD</CurrencyCode><FormattedPrice>$4.00</FormattedPrice></LowestUsedPrice><TotalNew>0</TotalNew><TotalUsed>2</TotalUsed><TotalCollectible>0</TotalCollectible><TotalRefurbished>0</TotalRefurbished></OfferSummary><EditorialReviews><EditorialReview><Source>Product Description</Source><Content>424 pages</Content></EditorialReview></EditorialReviews></Item><Item><ASIN>0155719653</ASIN><DetailPageURL>http://www.amazon.com/Private-Side-American-History-Readings/dp/0155719653%3FSubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D165953%26creativeASIN%3D0155719653</DetailPageURL><ItemLinks><ItemLink><Description>Technical Details</Description><URL>http://www.amazon.com/Private-Side-American-History-Readings/dp/tech-data/0155719653%3FSubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0155719653</URL></ItemLink><ItemLink><Description>Add To Baby Registry</Description><URL>http://www.amazon.com/gp/registry/baby/add-item.html%3Fasin.0%3D0155719653%26SubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0155719653</URL></ItemLink><ItemLink><Description>Add To Wedding Registry</Description><URL>http://www.amazon.com/gp/registry/wedding/add-item.html%3Fasin.0%3D0155719653%26SubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0155719653</URL></ItemLink><ItemLink><Description>Add To Wishlist</Description><URL>http://www.amazon.com/gp/registry/wishlist/add-item.html%3Fasin.0%3D0155719653%26SubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0155719653</URL></ItemLink><ItemLink><Description>Tell A Friend</Description><URL>http://www.amazon.com/gp/pdp/taf/0155719653%3FSubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0155719653</URL></ItemLink><ItemLink><Description>All Customer Reviews</Description><URL>http://www.amazon.com/review/product/0155719653%3FSubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0155719653</URL></ItemLink><ItemLink><Description>All Offers</Description><URL>http://www.amazon.com/gp/offer-listing/0155719653%3FSubscriptionId%3D084BT0EPNB27A07DGR82%26tag%3Dpragbook-20%26linkCode%3Dxm2%26camp%3D2025%26creative%3D386001%26creativeASIN%3D0155719653</URL></ItemLink></ItemLinks><SalesRank>6794037</SalesRank><ItemAttributes><Author>Thomas R., editor Frazier</Author><Binding>Paperback</Binding><EAN>9780155719651</EAN><EANList><EANListElement>9780155719651</EANListElement></EANList><Edition>2nd Edition</Edition><Format>Student Edition</Format><ISBN>0155719653</ISBN><Label>Harcourt Brace Jovanovich</Label><Manufacturer>Harcourt Brace Jovanovich</Manufacturer><NumberOfPages>424</NumberOfPages><PackageDimensions><Height Units="hundredths-inches">80</Height><Length Units="hundredths-inches">910</Length><Weight Units="hundredths-pounds">135</Weight><Width Units="hundredths-inches">600</Width></PackageDimensions><ProductGroup>Book</ProductGroup><ProductTypeName>BOOKS_1973_AND_LATER</ProductTypeName><PublicationDate>1979</PublicationDate><Publisher>Harcourt Brace Jovanovich</Publisher><SKU>CA-13-05-03-00038</SKU><Studio>Harcourt Brace Jovanovich</Studio><Title>The Private Side of American History: Readings in Everday Life, Second Edition (#2 since 1865)</Title></ItemAttributes><OfferSummary><LowestNewPrice><Amount>700</Amount><CurrencyCode>USD</CurrencyCode><FormattedPrice>$7.00</FormattedPrice></LowestNewPrice><LowestUsedPrice><Amount>12</Amount><CurrencyCode>USD</CurrencyCode><FormattedPrice>$0.12</FormattedPrice></LowestUsedPrice><TotalNew>1</TotalNew><TotalUsed>27</TotalUsed><TotalCollectible>0</TotalCollectible><TotalRefurbished>0</TotalRefurbished></OfferSummary><EditorialReviews><EditorialReview><Source>Product Description</Source><Content>Articles by 16 authors review social conditions in rural and urban America in the late 19th and 20th century.</Content></EditorialReview></EditorialReviews></Item></Items></ItemLookupResponse>

Open in new window

0
 
rmmarshAuthor Commented:
The problem is when I read it the first time looking for errors, it doesn't get reset to the beginning of the stream.  Therefore, it can't find the root element.

I know what is causing it, just don't know how to fix it!  :D
0
Fill in the form and get your FREE NFR key NOW!

Veeam is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

 
käµfm³d 👽Commented:
Ah. What is the type of sr that you are passing to the XmlTextReader constructor?
0
 
rmmarshAuthor Commented:
StreamReader
0
 
käµfm³d 👽Commented:
I'm not 100% on this, but try adding this before the second read:

sr.BaseStream.Position = 0;

Open in new window

0
 
rmmarshAuthor Commented:
Error:  This stream does not support seek operations.

Is there some other way to read a XML stream which would allow me to reposition to the beginning?

Also, using XMLTextReader, if I do a MoveToAttribute, can I go forward and backward using XMLTextReader?
0
 
käµfm³d 👽Commented:
I can't say that I've ever used the XmlBookmarkReader. Let me download it and try it out   = )
0
 
rmmarshAuthor Commented:
I think I just fixed it... needed to set bookmark immediately after first read... seems to work... thanks for your time... as usual, I appreciate it!
0
 
käµfm³d 👽Commented:
Awesome. Glad you got it worked out  = )
0
 
rmmarshAuthor Commented:
Not sure yet... have to send the program to the UK, where it fails... works in the US, but not the UK...

Go figure!

I'll get back to you as soon as I hear from him...
0
 
rmmarshAuthor Commented:
Nope, didn't work... same thing, missing root segment... I need to cogitate on this for a bit... I'll get back to you.
0
 
rmmarshAuthor Commented:
Took the code out that was causing the problem... I'll deal with any errors when they happen... thanks for your time.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 8
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now