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

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

500pts: My dataset (imported from XML) has an invalid Column - please help

Hi there,

Can anyone help?

I have imported an xml file into a dataset using ReadXML method. And it has worked, but its invented a new columnn name. So i am having problems binding it to a grid.

I don't understand why its invented a new column name... i would appreciate any input .. Thanks in advance...

Here a portion of the xml file...  as you can see .. there is a total of 4 columns
- <Cartera>
- <Record>
  <VAL>COMPANY A</VAL>
  <E>CBC456</E>
  <TI>928</TI>
  <VALUATION>19612222</VALUATION>
  </Record>
etc ,.... etc.....

Here is me printing out the values of the table in DEBUG - see number [4] it shouldn't be there, it is of course present in ALL rows and seems to equal {0} in all rows...
? myDataSet.Tables["Record"].Rows[5].ItemArray
{Length=5}
    [0]: "COMPANY A"
    [1]: "CBC456"
    [2]: "928"
    [3]: "19612222"
    [4]: {0}

? myDataSet.Tables["Record"].Columns[4].ColumnName
"Cartera_Id"
0
ianinspain
Asked:
ianinspain
  • 3
  • 2
1 Solution
 
rinksno1Commented:
string xmlfilepath = @"C:\Documents and Settings\Administrator\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\XMLFile1.xml";
            DataSet ds = new DataSet();
            ds.ReadXml(xmlfilepath);

            DataTable dtbl = ds.Tables[0];
            foreach (DataRow dr in dtbl.Rows)
            {
                Console.WriteLine(dr[0]);
                Console.WriteLine(dr[1]);
                Console.WriteLine(dr[2]);
                Console.WriteLine(dr[3]);
           
            }
            Console.ReadLine();

I did the above code on your xml but it is running perfect whenever i try to do dr[4] it gives me an error that column 4 not found...

thanks
rinks
0
 
ianinspainAuthor Commented:
Thanks rinksno1.. ermmm very strange... thanks for offering help here as it has me beat!! It is much appreciated.

I just did the same, and i still got number 4 :-) Maybe it will help if i show you more code and the real XML file rather than an example/portion?

All below is actual real data.

I changed your test a little and got this in the Console output

REPSOL YPF
ES0173516115
92845
1959029.50
0   /// THIS ISN'T on the XML file

my code below... is complete ... Get CarteraBolsas is a method that calls WebService that i wrote which returns XmlNode

the contents on tester.OuterXML - I copied and pasted it in the Command window..
? tester.OuterXml
"<response xmlns=\"\"><Error code=\"0\" description=\"OK\" /><CarteraDeBolsas><Record><VALOR>REPSOL YPF</VALOR><ISIN>ES0173516115</ISIN><TITULOS>92845</TITULOS><VALORACION>1959029.50</VALORACION></Record><Record><VALOR>FERROVIAL</VALOR><ISIN>ES0162601019</ISIN><TITULOS>19000</TITULOS><VALORACION>1091550</VALORACION></Record><Record><VALOR>GR.EMP.ENCE</VALOR><ISIN>ES0130625017</ISIN><TITULOS>39546</TITULOS><VALORACION>1166607</VALORACION></Record><Record><VALOR>ABENGOA</VALOR><ISIN>ES0105200416</ISIN><TITULOS>4000</TITULOS><VALORACION>74760</VALORACION></Record><Record><VALOR>BSCH</VALOR><ISIN>ES0113900J37</ISIN><TITULOS>429243</TITULOS><VALORACION>4700210.85</VALORACION></Record><Record><VALOR>ENDESA</VALOR><ISIN>ES0130670112</ISIN><TITULOS>124735</TITULOS><VALORACION>3319198.35</VALORACION></Record><Record><VALOR>ZELTIA</VALOR><ISIN>ES0184940817</ISIN><TITULOS>287208</TITULOS><VALORACION>1619853.12</VALORACION></Record><Record><VALOR>CEPSA</VALOR><ISIN>ES0132580319</ISIN><TITULOS>23000</TITULOS><VALORACION>12
76500</VALORACION></Record><Record><VALOR>BA.POPULAR</VALOR><ISIN>ES0113790531</ISIN><TITULOS>192039</TITULOS><VALORACION>2168120.31</VALORACION></Record><Record><VALOR>SOGECABLE</VALOR><ISIN>ES0178483139</ISIN><TITULOS>107624</TITULOS><VALORACION>2369880.48</VALORACION></Record><Record><VALOR>IBERDROLA</VALOR><ISIN>ES0144580018</ISIN><TITULOS>69000</TITULOS><VALORACION>1797450</VALORACION></Record><Record><VALOR>UNION FENOSA</VALOR><ISIN>ES0181380017</ISIN><TITULOS>25626</TITULOS><VALORACION>774673.98</VALORACION></Record><Record><VALOR>BANESTO</VALOR><ISIN>ES0113440038</ISIN><TITULOS>77560</TITULOS><VALORACION>1059469.60</VALORACION></Record><Record><VALOR>ARCELOR</VALOR><ISIN>LU0140205948</ISIN><TITULOS>30553</TITULOS><VALORACION>1073937.95</VALORACION></Record><Record><VALOR>BBVA</VALOR><ISIN>ES0113211835</ISIN><TITULOS>245443</TITULOS><VALORACION>3826456.37</VALORACION></Record><Record><VALOR>PRISA</VALOR><ISIN>ES0171743117</ISIN><TITULOS>60861</TITULOS><VALORACION>746764.47</VALORACION></Record><Record><
VALOR>GAMESA</VALOR><ISIN>ES0143416115</ISIN><TITULOS>30785</TITULOS><VALORACION>487326.55</VALORACION></Record><Record><VALOR>ALTADIS</VALOR><ISIN>ES0177040013</ISIN><TITULOS>55958</TITULOS><VALORACION>2044705.32</VALORACION></Record><Record><VALOR>ERCROS</VALOR><ISIN>ES0125140014</ISIN><TITULOS>1575143</TITULOS><VALORACION>992340.09</VALORACION></Record><Record><VALOR>OHL</VALOR><ISIN>ES0142090317</ISIN><TITULOS>24198</TITULOS><VALORACION>354016.74</VALORACION></Record><Record><VALOR>ACERINOX</VALOR><ISIN>ES0132105018</ISIN><TITULOS>151229</TITULOS><VALORACION>1979587.61</VALORACION></Record><Record><VALOR>FADESA</VALOR><ISIN>ES0135961219</ISIN><TITULOS>17800</TITULOS><VALORACION>449272</VALORACION></Record><Record><VALOR>LA SEDA BAR.</VALOR><ISIN>ES0175290115</ISIN><TITULOS>436034</TITULOS><VALORACION>967995.48</VALORACION></Record><Record><VALOR>INDRA A</VALOR><ISIN>ES0118594417</ISIN><TITULOS>125798</TITULOS><VALORACION>1937289.20</VALORACION></Record><Record><VALOR>CINTRA</VALOR><ISIN>ES0118900010</ISIN>
<TITULOS>59900</TITULOS><VALORACION>589416</VALORACION></Record><Record><VALOR>TELEFONICA</VALOR><ISIN>ES0178430E18</ISIN><TITULOS>384933</TITULOS><VALORACION>4981033.02</VALORACION></Record><Record><VALOR>INM.URBIS</VALOR><ISIN>ES0154800215</ISIN><TITULOS>48790</TITULOS><VALORACION>938231.70</VALORACION></Record><Record><VALOR>COR.MAPFRE</VALOR><ISIN>ES0124244C36</ISIN><TITULOS>72015</TITULOS><VALORACION>1053579.45</VALORACION></Record><Record><VALOR>BANKINTER</VALOR><ISIN>ES0113679338</ISIN><TITULOS>18784</TITULOS><VALORACION>910084.80</VALORACION></Record><Record><VALOR>METROVACESA</VALOR><ISIN>ES0154220414</ISIN><TITULOS>50</TITULOS><VALORACION>3712.50</VALORACION></Record><Record><VALOR>ACS</VALOR><ISIN>ES0167050915</ISIN><TITULOS>49562</TITULOS><VALORACION>1555255.56</VALORACION></Record></CarteraDeBolsas></response>"

and my code
                  DataSet myDataSet = new DataSet();

                  XmlNode tester = GetCarteraBolsas(); // this is a method that calls a webservice


                  XmlNodeReader testme = new XmlNodeReader(tester);


                  
                  myDataSet.ReadXml(testme);


                  DataTable dtbl = myDataSet.Tables["Record"]; // I need to ask for "Record" as my dataset contains more tables i.e. the Error table
                  foreach (DataRow dr in dtbl.Rows)
                  {
                        Console.WriteLine(dr[0]);
                        Console.WriteLine(dr[1]);
                        Console.WriteLine(dr[2]);
                        Console.WriteLine(dr[3]);
                        Console.WriteLine(dr[4]); //this still returns 0
           
                  }
0
 
ianinspainAuthor Commented:
and the contents of this new column/field is this

? myDataSet.Tables["Record"].Columns[4].ColumnName
"CarteraDeBolsas_Id"

it is as if its trying to create an automatic ID/Key?? but unknown why ... and why its putting 0 in there

0
 
ianinspainAuthor Commented:
Don't worry i got it going now... don't know what it was.. but your test helped so i am going to assign points

Cheers

Ian
0
 
rinksno1Commented:
:-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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