Reading XML

  • Thread starter Thread starter Tom Woods
  • Start date Start date
T

Tom Woods

I'm using C# VS2005 and need to access an XML file. I've setup an
XmlDocument which opens a file as the following:
<root>
<Row Run_Date="8/23/2007 11:55:10 AM" />
<Row Trade_Date="8/23/2007" />
<Row Supplemental_Data="3.1 : 3.3 : 3.5 : 3.7 : 3.9 : 4.2 : 4.9" />
<Row ARM_Model="On" />
</root>

What is the best way to access each element individually. For example, how
can I get the "Run_Date" or the "Supplemental_Data" from the document?

TIA,
Tom Woods
 
Tom,

You can use the XmlDocument class to load the document, and then use an
XPath expression passed to SelectSingleNode (in this case, for Run_Date, you
would pass "root/RunDate").

If there is a schema to this file which dictates the order of the rows,
then you might be able to use an XmlReader and just get the values for the
attributes as you move forward through the nodes, since you know the
position will be fixed.
 
I'm using C# VS2005 and need to access an XML file. I've setup an
XmlDocument which opens a file as the following:
<root>
<Row Run_Date="8/23/2007 11:55:10 AM" />
<Row Trade_Date="8/23/2007" />
<Row Supplemental_Data="3.1 : 3.3 : 3.5 : 3.7 : 3.9 : 4.2 : 4.9" />
<Row ARM_Model="On" />
</root>

What is the best way to access each element individually. For example, how
can I get the "Run_Date" or the "Supplemental_Data" from the document?

TIA,
Tom Woods

Run_Date and Supplement_Data are what are referred to in XML parlance
as Attributes. Attributes are accessed in the DOM slightly different
than nodes.

You will need to load the XML into an XmlDocument. Then do a
SelectNodes on Row, since it it a list of nodes. Then use a foreach
loop to loop through the XmlNodeList of Row, and then do a
SelectSingleNode on each one, but specify the path for the attributes
by preceeding the attribute name with the AT-Sign ("@").
Check to see if the return value for the SelectSingleNode for the
attribute is null. If null, the attribute was not present.

I really don't have time to key in sample code, especially bug-free
sample code. Hopefully you can figure out what to do from my
description.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top