PDA

Click to See Complete Forum and Search --> : Reading XML file


neodatatype
Feb 23rd, 2003, 10:52 AM
Hi all,

Can someone tell me how can I have direct access to XML element/attribute?

sample:

<project name="myproject" attribute "myattribute">
<myelement name="elementname" />
<nestedelement x="1" y="2">
<row>first row</row>
</nestedelement >
</project>


Task 1: I want to know the attribute "name" of element "project"

Task 2: I want to know the attribute "name" of element "myelement"

Task 3: I want to know the element string of element "row"

All I got from sites and tutorials is how to parse the XML file and list alla elements/attributes, but I don't need a parsing, I need to go directly to an element and get its attributes.

How?

Thank you!

CornedBee
Feb 24th, 2003, 05:57 AM
This is malformed XML.
It has to be attribute="myattribute".

But I guess that's just a typo.

Doesn't the framework have a complete XML parser and DOM?
Pseudocode:

project = document.getElementsByTagName("project")[0];
projname = project.getAttribute("name");

Lethal
Feb 28th, 2003, 05:12 PM
Here's an example that should get you started:



using System;
using System.Xml;

class XmlParseExample
{
static void Main()
{
XmlDocument dom = new XmlDocument();
string xml = "<car make='Audi' model='TT' year='2002' />";
dom.LoadXml(xml);

foreach(XmlAttribute attr in dom.DocumentElement.Attributes)
{
string s = "Name: " + attr.Name;
s += " Value: " + attr.Value;
Console.WriteLine(s);
Console.WriteLine();
}
}
}