Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XML Question

XML Question

Scheduled Pinned Locked Moved C#
questioncssxmlhelpannouncement
4 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    matthias s 0
    wrote on last edited by
    #1

    I've got a set of xml files which all look similar to this one: Standard Das Standard Layout. default.css standard.jpg Text Version Das Leichtgewicht. textonly.css textonly.jpg The number of item elements varies. What is the easiest way to loop through the xml and extract the individual data items? I had a look at the XmlTextReader but the methods provided in this class seem a bit cumbersum. There must be an easier way... Any help is greatly appreciated. /matthias

    C L S 3 Replies Last reply
    0
    • M matthias s 0

      I've got a set of xml files which all look similar to this one: Standard Das Standard Layout. default.css standard.jpg Text Version Das Leichtgewicht. textonly.css textonly.jpg The number of item elements varies. What is the easiest way to loop through the xml and extract the individual data items? I had a look at the XmlTextReader but the methods provided in this class seem a bit cumbersum. There must be an easier way... Any help is greatly appreciated. /matthias

      C Offline
      C Offline
      ChesterPoindexter
      wrote on last edited by
      #2

      Try using XPath XmlDocument document = new XmlDocument(); document.Load(filename); XmlNodeList nodes = document.SelectNodes("designs/item"); foreach(XmlNode node in nodes) { ... } something like that should go through the nodes...

      1 Reply Last reply
      0
      • M matthias s 0

        I've got a set of xml files which all look similar to this one: Standard Das Standard Layout. default.css standard.jpg Text Version Das Leichtgewicht. textonly.css textonly.jpg The number of item elements varies. What is the easiest way to loop through the xml and extract the individual data items? I had a look at the XmlTextReader but the methods provided in this class seem a bit cumbersum. There must be an easier way... Any help is greatly appreciated. /matthias

        L Offline
        L Offline
        LongRange Shooter
        wrote on last edited by
        #3

        Here is a simple process the goes through an entire XML file regardless of definition.

        		XmlTextReader reader = new XmlTextReader(@"..\\..\\test.xml");
        		try
        		{
        			while (!reader.EOF && reader.NodeType != System.Xml.XmlNodeType.Element)
        			{
        				BuildLine( reader, reader.NodeType.ToString(), reader.Name,  reader.Value );
        				reader.Read();
        			}
        			if (reader.EOF)
        			{
        				reader.Close();
        				results.Items.Add("End of file process");
        				return;
        			}
        		}
        		catch (System.Exception exc)
        		{
        			results.Items.Add(exc.Message);
        			if (exc.InnerException != null)
        				results.Items.Add(exc.InnerException.Message);
        			return;
        		}
        
        		int currentDepth = reader.Depth;
        		DoAttributes(reader);
        
        		do
        		{
        			if (reader.NodeType == System.Xml.XmlNodeType.Element)
        			{
        				results.Items.Add(BuildLine( reader, "Element", reader.Name,  reader.Value ));
        				if (reader.AttributeCount > 0)
        				{
        					DoAttributes(reader);
        				}
        				reader.Read();
        			}
        			else
        			{
        				results.Items.Add(BuildLine( reader, reader.NodeType.ToString(), reader.Name,  reader.Value ));
        				reader.Read();
        			}
        		} while (!reader.EOF 
        			&& !((reader.NodeType == System.Xml.XmlNodeType.EndElement 
        			|| reader.NodeType == System.Xml.XmlNodeType.Element) 
        			&& reader.Depth <= currentDepth));
        

        ...

        	private void DoAttributes(XmlTextReader reader)
        	{
        		results.Items.Add(BuildLine( reader, reader.NodeType.ToString(), reader.Name,  reader.Value ));
        		if (reader.MoveToFirstAttribute())
        		{
        			do
        			{
        				results.Items.Add(BuildLine( reader, "Attribute", reader.Name, reader.Value ));
        			}while(reader.MoveToNextAttribute());
        		}
        	}
        

        There are 10 kinds of people in the world.
        Those that read binary...
        ...and those who don't.

        1 Reply Last reply
        0
        • M matthias s 0

          I've got a set of xml files which all look similar to this one: Standard Das Standard Layout. default.css standard.jpg Text Version Das Leichtgewicht. textonly.css textonly.jpg The number of item elements varies. What is the easiest way to loop through the xml and extract the individual data items? I had a look at the XmlTextReader but the methods provided in this class seem a bit cumbersum. There must be an easier way... Any help is greatly appreciated. /matthias

          S Offline
          S Offline
          StoneTheCrows
          wrote on last edited by
          #4

          Both the suggestions submitted are good. If you want a good tutorial on working with XML try Here and look for Lesson 10: Working with XML (NB: The Tutorial is for Visual C# 2005 Express)

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups