General XML C# question (stuck).

Tim Field

Member
Joined
Aug 31, 2005
Messages
20
Hi All,

Firstly whats 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>


Id like to navigate to the UPN in this file so starting at the DocumentElement (root) is of no use as itll 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 Im doing Id 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;
}
}
}
 
Im 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:
C#:
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
 
Back
Top