Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
| Search | Today's Posts | Mark Forums Read
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 10th, 2006, 06:53 AM
Authorized User
 
Join Date: May 2004
Location: Blackpool, Lancashire, United Kingdom.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to sencee Send a message via AIM to sencee Send a message via MSN to sencee Send a message via Yahoo to sencee
Default Pogramming the XmlDOM in C#

Im new to using Xml with C#. I have been hacking around at one section for a day now and can't get it to work at all. Rather than confuse you with my shambles code, I will jsut explain the concept. Im also slightly confussed, or misslead with my understanding of the type deffinitions, and their methods for subtrees.


Code:
XML...
<Root>
   <MajorKey att="MAJattribute1">
      <MinorKey att="MINattribute1">
          <Value>value1</Value>
      </MinorKey>
      <MinorKey att="MINattribute2">
         <Value>value2</value>
      </MinorKey>
   </MajorKey>
   <MajorKey att="MAJattribute2">
      <MinorKey att="MINattribute3">
          <Value>value3</Value>
      </MinorKey>
      <MinorKey att="MINattribute4">
         <Value>value4</value>
      </MinorKey>
   </MajorKey>
</Root>
Im having trouble getting it to create a nodelist to children underneath a node based on it's att (attribute) value. Also, I want to extract that attribute value into a string so "MAJattribute1" would gointo a string inside the loop for that node treversal.

Any help, or suggestions.

I have spent hours and hours reading, and cant seem to find anything to pinpoint this...only ever creating attributes indide a single node, not looping through based on the attribute value.

IM GETTING STRESSED

Regards
njc

 
Old March 10th, 2006, 07:08 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Can you post an example of your desired output?

--

Joe (Microsoft MVP - XML)
 
Old March 10th, 2006, 08:29 AM
Authorized User
 
Join Date: May 2004
Location: Blackpool, Lancashire, United Kingdom.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to sencee Send a message via AIM to sencee Send a message via MSN to sencee Send a message via Yahoo to sencee
Default

For the minutes, just a variable inside the loops

PSEUDO
Code:
Loop
XmlNodeList MajorKeyNodeList

ForEach (MajorKey in MajorKeyNodeList)
      string varMajorKey = the contents of the majorkey attribute (varKey=MAJattribute1)
      XmlNodeList MinorKeyNodeList (a list of all the MinorKeys under current Majorkey)
      ForEach (MinorKey in MinorKeyNodeList)
          string varMinorKey = content MinorKey attribute
          string CompleteKey = varMajorKey+varMinorKey
  
          XmlNodeList ValuesNodeList (a list of all the value elements)

          ForEach (Value in ValueNodeList)
             string varValue = CurrentElement.InnerText

etc
  
please excuse the shabby use of half attmpted failed syntax/half pesudo.
 
Old March 10th, 2006, 09:37 AM
Authorized User
 
Join Date: May 2004
Location: Blackpool, Lancashire, United Kingdom.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to sencee Send a message via AIM to sencee Send a message via MSN to sencee Send a message via Yahoo to sencee
Default

I'd really appreciate any help on this Joe. Im bubbling :)

 
Old March 10th, 2006, 10:14 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

Code:
using System;
using System.Xml;

namespace DomNodeList
{
  class Program
  {
    static void Main(string[] args)
    {
      XmlDocument doc = new XmlDocument();
      doc.Load(@"D:\workjjf\scripts\xml\MajorKeys.xml"); //<path to XML here>
      XmlNodeList majorKeyList = doc.SelectNodes(@"/Root/MajorKey");
      foreach (XmlNode majorKeyNode in majorKeyList)
      {
        XmlElement majorElement = majorKeyNode as XmlElement;
        string majorAttValue = "<null>";
        if (majorElement != null)
        {
          string attValue = majorElement.GetAttribute("att");
          if (attValue != null)
          {
            majorAttValue = attValue;
          }
        }
        Console.WriteLine(majorAttValue);
        XmlNodeList minorKeyList = majorKeyNode.SelectNodes(@"MinorKey");
        foreach (XmlNode minorKeyNode in minorKeyList)
        {
          XmlElement minorElement = minorKeyNode as XmlElement;
          string minorAttValue = "<null>";
          if (minorElement != null)
          {
            string attValue2 = minorElement.GetAttribute("att");
            if (attValue2 != null)
            {
              minorAttValue = attValue2;
            }
          }
          Console.WriteLine("\t" + minorAttValue);
          XmlNodeList valueList = minorElement.SelectNodes("Value");
          foreach (XmlNode valueNode in valueList)
          {
            Console.WriteLine("\t\t" + valueNode.InnerText);
          }
        }
      }
      Console.ReadLine();
    }
  }
}
--

Joe (Microsoft MVP - XML)
 
Old March 10th, 2006, 11:09 AM
Authorized User
 
Join Date: May 2004
Location: Blackpool, Lancashire, United Kingdom.
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to sencee Send a message via AIM to sencee Send a message via MSN to sencee Send a message via Yahoo to sencee
Default

Thanks Joe, however, I had just figured it out!
I really aprreciate your effort, exactly the same as what I ended up with, apart from mine is event based.

Mucho Appreciated.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Microsoft.XMLDOM Alternative? gabster XSLT 4 September 11th, 2007 02:30 AM
JavaScript and XMLDOM kirthi97 XML 4 June 27th, 2006 02:22 AM
XMLDOM to load from URL?? lian_a Classic ASP XML 0 January 19th, 2005 10:15 PM
XMLDOM problem braindog_43 Javascript How-To 2 January 16th, 2005 01:54 PM
XMLDOM problem braindog_43 XML 2 January 16th, 2005 01:38 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.