6 Months Free & No Setup Fees ASP.NET Hosting!
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » XML format not filling data table correct
       
Author Reply
mike Delvotti
posted 63 posts
since Aug 14, 2008 
from

XML format not filling data table correct

  Posted on: 22 Feb 2012       
Reading xml format is not a strong point of mine, and although i am reading and display an xml file my code seems to stop and jump to the first line of the xml?

the format i'm trying to read is as below:

<?xml version="1.0" encoding="ISO-8859-1" ?>
- <result id="xxxxx" generated="xxxx" mode="live" account_id="000">
  <vrm>AXXXXX</vrm> 
  <make>FORD</make>
  <model>FIESTA FREESTYLE</model>
- <valuation db="201202" mileage="110000">
- <match id="92">
  <make>FORD</make>
  <model>FIESTA (1999-02)</model>
  <variation>5DR HATCHBACK 1.25 FREESTYLE</variation>

I'm using the following code to read and display into a datagrid using http WebRequest.

DataSet

dsLookup.ReadXml(response.GetResponseStream());


dsLookup = new DataSet()


dataGridView1.DataSource = dsLookup.Tables[0];

The problem is, it reads down to this line then stops:

- <valuation db="201202" mileage="110000">

even if i try to go past and display into a text field like below it still doesn't read past that problem line? is there a way to bypass the line above?

value_retailTextBox.Text = dsLookup.Tables[0].Rows[0][5].ToString();



Senthilkumar
posted  1057 posts
since  Jul 28, 2009 
from  Bangalore

 Re: XML format not filling data table correct
  Posted on: 23 Feb 2012        0  
Please verify the XML tags formed correctly. Because when you convert the dataset table into XML then it will be in the XML standard.

Here am not seeing some of the tags are not closed properly. That means the XML parsing will be failed in the DataSet ReadXML function.
If this post is useful then mark it as "Accepted Answer"
mike Delvotti
posted  63 posts
since  Aug 14, 2008 
from 

 Re: XML format not filling data table correct
  Posted on: 23 Feb 2012        0  
Hi, thanks for the reply, the full format of the xml is:

<?xml version="1.0" encoding="ISO-8859-1" ?>
- <result id="xxxxx" generated="xxxx" mode="live" account_id="000">
    <vrm>AXXXXX</vrm>
    <make>FORD</make> 
    <model>FIESTA FREESTYLE</model>
- <valuation db="201202" mileage="110000">
-    <match id="xx">
         <make>FORD</make> 
         <model>FIESTA (1999-02)</model>
     <variation>5DR HATCHBACK 1.25 FREESTYLE</variation>
     <new>16366</new> 
     <retail>5000</retail>
   </match>
  </valuation>
  </result>

I've just noticed that the line it stops at is an element. so what i really would like to do is display the element "new" + "retail" in textbox1 & textbox2, those are within valuation/match elements. I assume i have to load the xml without using a dataset then?
mike Delvotti
posted  63 posts
since  Aug 14, 2008 
from 

 Re: XML format not filling data table correct
  Posted on: 24 Feb 2012        0  
Guys, i have worked out part of the issue, but just wondered if anyone has a work around for my code below?

I'm reading the xml that is collected from the web (via another source) now after some experimenting it's the hyphen in front of the attribute causing the code to stop reading the xml as the first and second offending line below:

 - <valuation db="201202" mileage="70000">
- <match id="xxxx"> 
  <new>16366</new>
  <retail>5000</retail>
  <trade>3225</trade>
  <average>2600</average>
  <rtr>3450</rtr>
  <cat_d>1700</cat_d>
  </match>
  </valuation>

if i remove the hyphen ("-") from the front of the attribute my code below works just fine:

                              var valuation = from match in xmlDoc.Descendants("match")
                              select new
                              {
                                  retail = match.Element("trade").Value,
                                
                              };

               
                            foreach (var match in valuation)
                            {
                                value_retailTextBox.Text = match.retail;
                   

                             }

I guess I'm asking if the is a way i can modify my code to remove the hyphen from in front of the attributes?

Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: XML format not filling data table correct
  Posted on: 24 Feb 2012   Accepted Answer     1  
What I think you need to do here is to convert the stream to text, remove the offending hyphens and parse the cleaned-up text into an XDocument. So, I'd try this:

      Stream s = response.GetResponseStream();
      System.IO.StreamReader sr = new System.IO.StreamReader(s);
      string xml = sr.ReadToEnd();
      sr.Close();
      xml = xml.Replace("- ", "  "); // get rid of leading hyphens
      XDocument xmlDoc = XDocument.Parse(xml); 

You should then be able to use xmlDoc to read the elements you need using the LINQ to XML code you already have. 
Jay Morris
posted  547 posts
since  Jan 25, 2011 
from  Manchester

 Re: XML format not filling data table correct
  Posted on: 24 Feb 2012        0  
Have a look at filehelpers

http://www.filehelpers.com/

A man with one watch knows what time it is; a man with two watches is never quite sure.
mike Delvotti
posted  63 posts
since  Aug 14, 2008 
from 

 Re: XML format not filling data table correct
  Posted on: 27 Feb 2012        0  
Jay - thanks for the link, I'll keep that in mind.

Vulpes - once again that is a bang on solution and works a treat, I have one last question on this subject that you will no doubt see a simple way, the returns that display in my textbox I would really like to format as "F2" what would be the best way for me to achieve that in code, I tried it like below but it doesn't like my method ;(

 var valuation = from match in xmlDoc.Descendants("match")
                              select new
                              {
                                  retail = match.Element("retail").Value.ToString("F2"),// my sample here that doesn't work for F2 format?
                                  trade = match.Element("trade").Value,
                                  average = match.Element("average").Value,
                                  firstnew = match.Element("new").Value,
                                 
                              };

               
                foreach (var match in valuation)
                {

                    value_retailTextBox.Text = match.retail;
                    value_tradeTextBox.Text = match.trade;
                    value_averageTextBox.Text = match.average;
                    value_newTextBox.Text = match.firstnew;
                  

                }

Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: XML format not filling data table correct
  Posted on: 27 Feb 2012        0  
Yeah, the "F2" format only works if you have something which is already a number.

I'd try the following instead which also deals with the possibility that the element could be blank or contain an invalid number (it then uses zero instead):


         var valuation = from match in xmlDoc.Descendants("match")
         select new
         {
            retail = match.Element("retail").Value,
            trade = match.Element("trade").Value,
            average = match.Element("average").Value,
            firstnew = match.Element("new").Value,                                 
         };

                
         foreach (var match in valuation)
         {
            decimal temp;
            decimal.TryParse(match.retail, out temp);
            value_retailTextBox.Text = temp.ToString("F2");
            value_tradeTextBox.Text = match.trade;
            value_averageTextBox.Text = match.average;
            value_newTextBox.Text = match.firstnew;                 
         }
mike Delvotti
posted  63 posts
since  Aug 14, 2008 
from 

 Re: XML format not filling data table correct
  Posted on: 27 Feb 2012        0  

Perfect - Thanks Vulpes.
       
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved