|
-
Feb 23rd, 2003, 11:52 AM
#1
Thread Starter
Lively Member
Reading XML file
Hi all,
Can someone tell me how can I have direct access to XML element/attribute?
sample:
Code:
<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!
-
Feb 24th, 2003, 06:57 AM
#2
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:
Code:
project = document.getElementsByTagName("project")[0];
projname = project.getAttribute("name");
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 28th, 2003, 06:12 PM
#3
PowerPoster
Here's an example that should get you started:
Code:
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();
}
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|