As we have seen so far, both the XPath language and the .NET Framework provide various means to access an attribute. For example, the XPath language uses the @ sign to access an XML attribute while the XmlNode class is equipped with the OuterXml, the InnerXml, and the InnerText properties. Here is an example that uses the XmlNode.InnerXml property to access the attribute: using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
Label lblActorsRoles;
ListBox lbxActorsRoles;
public Exercise()
{
lblActorsRoles = new Label();
lblActorsRoles.AutoSize = true;
lblActorsRoles.Text = "Characters in Videos";
lblActorsRoles.Location = new Point(12, 10);
Controls.Add(lblActorsRoles);
lbxActorsRoles = new ListBox();
lbxActorsRoles.Size = new System.Drawing.Size(250, 220);
lbxActorsRoles.Location = new Point(12, 28);
Controls.Add(lbxActorsRoles);
Text = "Video Collection";
XmlDocument xdVideos = new XmlDocument();
xdVideos.Load("../../Videos.xml");
XmlElement xeVideo = xdVideos.DocumentElement;
XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/cast-members/actor/@role");
foreach(XmlNode xnVideo in xnlVideos)
lbxActorsRoles.Items.Add(xnVideo.InnerXml);
}
[STAThread]
public static int Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Exercise());
return 0;
}
}
This would produce:
Remember that, to get all attributes, you can use @* as in: XmlElement xeVideo = xdVideos.DocumentElement;
XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/cast-members/actor/@*");
foreach(XmlNode xnVideo in xnlVideos)
lbxActorsRoles.Items.Add(xnVideo.InnerXml);
Notice that the XmlNode.InnerXml property applied to an attribute produces only the text of the attribute. In the same way, you can use the XmlNode.InnerText property to get the same result. On the other hand, the XmlNode.OuterXml property produces the name and value of an attribute. Here is an example: XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/cast-members/actor/@role");
foreach(XmlNode xnVideo in xnlVideos)
lbxActorsRoles.Items.Add(xnVideo.OuterXml);
This would produce:
In some cases, you may want to get both the attribute and its parent. To do that, after the name of the element, include the @ and name of the attribute in square brackets. Here is an example: using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
Label lblActorsRoles;
ListBox lbxActorsRoles;
public Exercise()
{
lblActorsRoles = new Label();
lblActorsRoles.AutoSize = true;
lblActorsRoles.Text = "Characters in Videos";
lblActorsRoles.Location = new Point(12, 10);
Controls.Add(lblActorsRoles);
lbxActorsRoles = new ListBox();
lbxActorsRoles.Size = new System.Drawing.Size(325, 220);
lbxActorsRoles.Location = new Point(12, 28);
Controls.Add(lbxActorsRoles);
Text = "Video Collection";
XmlDocument xdVideos = new XmlDocument();
xdVideos.Load("../../Videos.xml");
XmlElement xeVideo = xdVideos.DocumentElement;
XmlNodeList xnlVideos =
xeVideo.SelectNodes("/videos/video/cast-members/actor[@role]");
foreach(XmlNode xnVideo in xnlVideos)
lbxActorsRoles.Items.Add(xnVideo.OuterXml);
Size = new System.Drawing.Size(358, 280);
}
[STAThread]
public static int Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Exercise());
return 0;
}
}
This would produce:
If you want to get all attributes and their parent, remember that you can use * in place of the name of the attribute. Here is an example: using System;
using System.Xml;
using System.Drawing;
using System.Windows.Forms;
public class Exercise : Form
{
Label lblActorsRoles;
ListBox lbxActorsRoles;
public Exercise()
{
lblActorsRoles = new Label();
lblActorsRoles.AutoSize = true;
lblActorsRoles.Text = "Characters in Videos";
lblActorsRoles.Location = new Point(12, 10);
Controls.Add(lblActorsRoles);
lbxActorsRoles = new ListBox();
lbxActorsRoles.Size = new System.Drawing.Size(655, 50);
lbxActorsRoles.Location = new Point(12, 28);
Controls.Add(lbxActorsRoles);
Text = "Video Collection";
XmlDocument xdVideos = new XmlDocument();
xdVideos.Load("../../Videos.xml");
XmlElement xeVideo = xdVideos.DocumentElement;
XmlNodeList xnlVideos = xeVideo.SelectNodes("/videos/video/title[@*]");
foreach(XmlNode xnVideo in xnlVideos)
lbxActorsRoles.Items.Add(xnVideo.OuterXml);
Size = new System.Drawing.Size(688, 110);
}
[STAThread]
public static int Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Exercise());
return 0;
}
}
This would produce:
|
|
|||||
|
|