Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi All,

 

Firstly what's the tag for displaying code properly in these messages?

<code> </code> or similar?

 

<ADTfile>

<Message>

<Header>

<DocumentName>A File</DocumentName>

<ATversion>2.3</ATversion>

</Header>

</Message>

<Pupils>

 

-- NOTE: lots of these.

<Pupil>

<UPN>F938870105001</UPN>

</Pupil>

</Pupils>

</ADTfile>

 

 

I'd like to navigate to the UPN in this file so starting at the DocumentElement (root) is of no use as it'll just go through the Message and Header etc. then never get to Pupils. I assume there is an easier way to do this (xpath??). If you can see what I'm doing I'd appreciate a few pointers. Thanks, Tim :D

 

private void ModifyWithChildren()

{

// If there are any child nodes

// Call this procedure recursively

if (xnod.HasChildNodes)

{

xnod = xnod.FirstChild;

string myname = xnod.Name;

ModifyWithChildren();

}

 

while(xnod != null)

{

DoXMLUpdate(xnod);

if (xnod.NextSibling != null)

{

xnod = xnod.NextSibling;

}

}

}

  • *Experts*
Posted

I'm not sure what your end goal is, but if you just want the generic code to get a list of Pupils into a collection so that you can loop through then try the following. Paste this over Class1 in a new console project:

using System;
using System.Xml;
namespace XmlTest
{
class Class1
{
	[sTAThread]
	static void Main(string[] args)
	{
		string s = string.Empty;
		s += "<ADTfile>";
		s += "	<Message>";
		s += "		<Header>";
		s += "			<DocumentName>A File</DocumentName>";
		s += "			<ATversion>2.3</ATversion>";
		s += "		</Header>";
		s += "	</Message>";
		s += "	<Pupils>";
		s += "		<Pupil>";
		s += "			<UPN>F938870105001</UPN>";
		s += "		</Pupil>";
		s += "		<Pupil>";
		s += "			<UPN>ABC</UPN>";
		s += "		</Pupil>";
		s += "		<Pupil>";
		s += "			<UPN>123</UPN>";
		s += "		</Pupil>";
		s += "	</Pupils>";
		s += "</ADTfile>";

		XmlDocument xml = new XmlDocument();
		xml.LoadXml(s);

		XmlNodeList pupils = xml.SelectNodes("/ADTfile/Pupils/Pupil/UPN");
		foreach(XmlNode pupil in pupils)
		{
			System.Diagnostics.Debug.WriteLine(pupil.InnerText);
		}
	}
}
}

 

The key bit is the XmlNodeList and the XPath expression "/ADTfile/Pupils/Pupil/UPN".

 

Also, for generic "stuff" you want to format, use <code> and </code> but use square brackets. I used "cs" in square brackets for C#, you can use "VB" in square brackets for VB.NET.

 

-ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...