Results 1 to 3 of 3

Thread: Reading XML file

  1. #1

    Thread Starter
    Lively Member neodatatype's Avatar
    Join Date
    Aug 2002
    Location
    Italy
    Posts
    103

    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!
    > NeoDataType.net <

    Try my Free .Net Reporting Tool!

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  3. #3
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    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
  •  



Click Here to Expand Forum to Full Width