Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

DataSet.ReadXml and Attributes

Question posted by: Brent (Guest) on April 10th, 2008 02:25 AM
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(sr);

DataSet ds = new DataSet();

ds.ReadXml(xmlReader);

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.GetValue(0); //<--DOES NOT WORK

}

Martin Honnen's Avatar
Martin Honnen
Guest
n/a Posts
April 10th, 2008
11:35 AM
#2

Re: DataSet.ReadXml and Attributes
Brent wrote:
Quote:
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("file.xml");
DataTable file = ds.Tables["file"];
foreach (DataRow row in file.Rows)
{
Console.WriteLine(row[0]);
}
to output

file1
file2


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/

Brent's Avatar
Brent
Guest
n/a Posts
June 27th, 2008
07:20 PM
#3

Re: DataSet.ReadXml and Attributes
On Apr 10, 4:30 am, Martin Honnen <mahotr...@yahoo.dewrote:
Quote:
Brent wrote:
Quote:
I'm trying to figure out which column an XML attribute belongs to when
read into a dataset. Take the following XML fragment:

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

>
Quote:
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("file.xml");
DataTable file = ds.Tables["file"];
foreach (DataRow row in file.Rows)
{
Console.WriteLine(row[0]);
}
to output
>
file1
file2
>
--
>
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/


Thank you. That did it!

--Brent

 
Not the answer you were looking for? Post your question . . .
189,846 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
  • Didn't find the answer you were looking for?
    Post Your Question
  • Top Community Contributors