DataSet.ReadXml and Attributes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brent

    DataSet.ReadXml and Attributes

    I'm trying to figure out which column an XML attribute belongs to when
    read into a dataset. Take the following XML fragment:


    <FileList>
    <file name="file1"></file>
    <file name="file2"></file>
    </FileList>

    How would I get out the file name?

    I have the code below so far.

    Thank for any help!

    --Brent

    =============== =============== ======
    //after successfully getting a bit of XML from http

    XmlTextReader xmlReader = new XmlTextReader(s r);

    DataSet ds = new DataSet();

    ds.ReadXml(xmlR eader);

    DataTable tableFiles = ds.Tables["FileList"];

    for (int i = 0; i < tableFiles.Rows .Count; i++)
    {
    object[] fileRow = tableFiles.Rows[i].ItemArray;

    //Here's where I can't seem to get the XML attribute
    //What "column" is the XML "name" attribute located in?

    string fileName = fileRow.GetValu e(0); //<--DOES NOT WORK

    }

  • Martin Honnen

    #2
    Re: DataSet.ReadXml and Attributes

    Brent wrote:
    I'm trying to figure out which column an XML attribute belongs to when
    read into a dataset. Take the following XML fragment:
    >
    >
    <FileList>
    <file name="file1"></file>
    <file name="file2"></file>
    </FileList>
    >
    How would I get out the file name?
    If you read that XML into a DataSet then the DataSet has one table named
    'file' which has one column named 'name' and two rows of data so you
    could use
    DataSet ds = new DataSet();
    ds.ReadXml("fil e.xml");
    DataTable file = ds.Tables["file"];
    foreach (DataRow row in file.Rows)
    {
    Console.WriteLi ne(row[0]);
    }
    to output

    file1
    file2


    --

    Martin Honnen --- MVP XML

    Comment

    • Brent

      #3
      Re: DataSet.ReadXml and Attributes

      On Apr 10, 4:30 am, Martin Honnen <mahotr...@yaho o.dewrote:
      Brent wrote:
      I'm trying to figure out which column an XML attribute belongs to when
      read into a dataset. Take the following XML fragment:
      >
      <FileList>
      <file name="file1"></file>
      <file name="file2"></file>
      </FileList>
      >
      How would I get out the file name?
      >
      If you read that XML into a DataSet then the DataSet has one table named
      'file' which has one column named 'name' and two rows of data so you
      could use
      DataSet ds = new DataSet();
      ds.ReadXml("fil e.xml");
      DataTable file = ds.Tables["file"];
      foreach (DataRow row in file.Rows)
      {
      Console.WriteLi ne(row[0]);
      }
      to output
      >
      file1
      file2
      >
      --
      >
      Martin Honnen --- MVP XML
      http://JavaScript.FAQTs.com/
      Thank you. That did it!

      --Brent

      Comment

      Working...