Results 1 to 9 of 9

Thread: reading xml data

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    Unhappy reading xml data

    i am a newbie to c#. but less of a new to vb6.

    i have been asked to write an app that goes through a folder containing xml files.

    i need to read the info held in the nodes into a datagrid

    i thought i might be able to use the authors project that is used as a walkthrough example in the .net help, but i can't seem to get it to work.

    can anyone point me/give me a v simple example of reading node data from xml files.

    even if it just works for one file that would be a start.
    but if it could iterate through folders that would be even better

    any help would be really appreciated

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    Unhappy

    I'm trying to use the authors example tht is in the c# walkthrough in vs.net

    what i have done is kept the original example app exactly the same, but just renamed one of my xml files as Authors.xml
    just to see if it processes the file.

    but i get an error:

    An unhandled exception of type 'System.ArgumentException' occurred in system.windows.forms.dll

    Additional information: Cannot create a child list for field Authors.


    and the debugger highlights this line of code in the bt_ReadXml_click


    dataGrid1.CaptionText = dataGrid1.DataMember;


    below is an example of the xml iam trying to read. can anyone give me a suggestion as to what to do. Is it the xml that i'm trying to read or is it my code?

    any help would be most appreciated

    <?xml version="1.0" encoding="UTF-16" standalone="no"?><!--Document created automatically by --><Specification TimeZone="GMT">
    <Header>
    <CustomerNumber>805105</CustomerNumber>
    <Password>1w4282t61</Password>
    <Scope>1</Scope>
    <CustomerName>Calypso Soft Drinks Ltd</CustomerName>
    <SubscriberCode>ACAL001</SubscriberCode>
    <Ean>5010228012919</Ean>
    </Header>
    <DataVersion Number="1.02.00" />
    <Message DateTime="2003-06-03T14:44:05" /><ExtractStatus><ProductCount>0</ProductCount><RenderTime>0h 0m 0s</RenderTime></ExtractStatus></Specification>

  3. #3
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    You can use a DataSet to read in the Xml file, then bind the dataset to a datagrid.
    Code:
    DataSet ds = new DataSet();
    ds.ReadXml("Authors.xml");
    dataGrid1.DataSource = ds.Tables[0].DefaultView;
    That should get you started.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    cheers managed to read a single xml file. to a datagrid

    now i need to figure out how to do a whole folders.

    in vb6 i would use the filesystemobject to get an array of all the files in a folder and then iterate through it

    what is similar to the filesystemobject in c#?

  5. #5
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Check out the System.IO namespace. It has everything you need.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    Unhappy

    trying to iterate through folder reading each xml file int turn. it seems to work okay for a bit and then throws this exception.

    An unhandled exception of type 'System.Xml.XmlException' occurred in system.xml.dll

    Additional information: System error.

    the debuger stops on this line:

    ds.ReadXml(directoryEntries[arrayIndex]);


    below is the code i've put under the button that reads the xml
    any help would really be appreciated.


    private void button1_Click(object sender, System.EventArgs e)
    {
    DataSet ds = new DataSet();
    string pathStr = @"\\Server1\COMMON\UDEXPierHouse\UDEXTransfer\Fails";
    string wholeStr;
    string [] directoryEntries = System.IO.Directory.GetFileSystemEntries(pathStr);
    int arrayIndex = 0;

    foreach (String str in directoryEntries)
    {
    System.Console.WriteLine (str);
    wholeStr = directoryEntries[arrayIndex];
    ds.ReadXml(directoryEntries[arrayIndex]);
    dataGrid2.DataSource = ds.Tables[0].DefaultView;
    dataGrid1.DataSource = ds.Tables[1].DefaultView ;
    arrayIndex++;
    }


    }

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    the error kept happening on the 34th index of the array so i removed the file that 34 refered to, to see if it was the file that was causing the error, but it wasn't

    now with the file removed it errors on line 33

    i know it must be an xml error of some sort, i just don't have the first clue how to stop it.

    any help would be really appreciated, this is my first proper use of c# so i'm sure i've made an elementary mistake

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405

    Smile

    the erro was because of the files in the 180 i check, about 3 were blank files which is why i got the exception.

    so i've removed the offending files and it no longer errors.

    my new problem is this at the moment datagrid1 only holds the info for the last file read, because i assum that in my for loop i am putting the new data into the same row of the datagrid each time.

    so my question is how do i increment the datagrid rows so that i insert data from each new file into a new row.

    i have included my code below.

    Please any help on this would be brilliant. i seem to be very near to sorting this.


    DataSet ds = new DataSet();
    string pathStr = @"\\Server\COMMON\UDEXPierHouse\UDEXTransfer\Fails";
    string wholeStr;
    string [] directoryEntries = System.IO.Directory.GetFileSystemEntries(pathStr);
    int arrayIndex = 0;


    foreach (String str in directoryEntries) {
    System.Console.WriteLine (str);
    wholeStr = directoryEntries[arrayIndex];

    ds.ReadXml(directoryEntries[arrayIndex]);
    dataGrid2.DataSource = ds.Tables[0].DefaultView;
    dataGrid1.DataSource = ds.Tables[1].DefaultView ;
    arrayIndex++;
    }

    listBox1.DataSource = directoryEntries;

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2002
    Location
    Norwich, UK
    Posts
    405
    i still have no joy.

    surely there must be a really simple way of adding a new row in my code for each iteration so i get results like

    file1 element1 element 2 element 3
    file2 element1 element 2 element 3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width