473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Shell Game with XML Attributes

AJ
I have a problem that I suspect shouldn't be this hard to figure out,
but I'm a .NET newbie so I'm looking for help.

I am reading from an XML file to populate a combo box with the value
of
an "Name" attribute to several elements. When the user selects an item
from the combo and clicks a command button, my application needs to:

1. Find the parent element of the "Name" attribute
2. Find the value of the "ID" attribute of the same element
3. Write the value of the ID attribute to another XML file.

I can do everything except steps 1 and 2 ;) I've investigated
XMLAttribute.OwnerElement and
ParentElement.Attributes(AttributeName).InnerText, but what I'm
missing
is how to assign the text value of the selected item in the combo box
to an Attribute object. In other words, how do I convert a String to
an
XmlAttribute? (Or an XmlElement, for that matter...)

Any help is appreciated.

Nov 12 '05 #1
2 1179
"AJ" <me@who.knows.where.com> wrote in message news:9q********************************@4ax.com...
I am reading from an XML file to populate a combo box with the value
of an "Name" attribute to several elements. When the user selects an item
from the combo and clicks a command button, my application needs to:

3. Write the value of the ID attribute to another XML file.

I can do everything except steps 1 and 2 ;) I've investigated
XMLAttribute.OwnerElement and
ParentElement.Attributes(AttributeName).InnerText, but what I'm
missing
An XmlAttribute is an XmlNode so it has to have a ParentNode property,
but that's always null because an attribute in XML technically doesn't have
a "parent".
1. Find the parent element of the "Name" attribute
What I see this as asking is either to identify the attribute's OwnerElement,
or perhaps the OwnerElement's ParentNode (?).

I'm going to assume OwnerElement for simplicity, that is your XML element
looks something like this,

<owner name="SelectedValue" id="excalibur-101" />
2. Find the value of the "ID" attribute of the same element
The XmlAttribute, 'Name', is going to be in the same AttributesCollection as
the 'ID' one, so if you are after the OwnerElement then you just get this
collection from its Attributes property.

// Assuming user selected "SelectedValue" in the combo box.

// The following XPath expression finds all XmlElements that
// have a Name attribute whose value is 'SelectedValue'.
//
XmlNodeList nodes = xmlDoc.SelectNodes( "//*[@Name='SelectedValue']");
foreach( XmlNode node in nodes )
{
if ( node.NodeType == XmlNodeType.Element )
{
XmlElement ownerElement = node as XmlElement;
XmlAttribute idAttr = ownerElement.Attributes.ItemOf( "ID");
// Do something with idAttr.
}
}
In other words, how do I convert a String to an XmlAttribute?
(Or an XmlElement, for that matter...)


To answer this question, you would call CreateAttribute( ) on the
XmlDocument and then assign the String to this XmlAttribute's
Value property. Finally, append the XmlAttribute to the Attributes
collection of the element you want it associated with.

To convert a String to an XmlElement, call CreateElement( ) on
the XmlDocument and then call CreateText( ) on XmlDocument
passing the String. On the XmlElement, call AppendChild( ) to
attach the XmlTextNode to the element. Finally, append the Xml-
Element into the XmlDocument at the appropriate place.
Derek Harmon
Nov 12 '05 #2
Thanks for the reply.

Turns out that XPath was the direction I needed to go. Was able at last
to do everything I needed with one line of code:

Dim selectedIDAttribute As XmlNode =
PrefsDoc.SelectSingleNode("/Root/EventsNode/Event[@Name='" & AttrName &
"']/@ID")

Nov 12 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Siemel Naran | last post by:
Hi. I'm writing a command shell that reads commands from standard input. At this point I have the command in a std::string. Now I want to execute this command in the shell. From the Borland...
4
by: Daedric | last post by:
Hello and thanks in advance to anyone who offers help. To make this simple, let's say I have a game which has 100 different monsters. I want a binary data file to hold all of these. It would...
1
by: tomtiv | last post by:
Hi All, I am starting to build a hockey game simulator. I am looking for any resources you know of to help me build this application, Specifically these topics. - AI for game play (Text...
1
by: David Kanter | last post by:
Hi, I have some VB scripts which start by opening an application, in this case it is a game called FEAR (some of you may be familiar). I am using Visual Studio 2005 Professional Edition running...
1
by: James Maeding | last post by:
I was looking for the easiest way to add an item to explorer's right click menus, and I found what looks to be a goldmine. There is a vb.net project available on:...
10
by: Max Kubierschky | last post by:
Hello, I'm planning to give a game programming course for kids of mixed age. For this, I am looking for an open source 2D game development kit. I am also willing to participate in the...
14
keyvanrahmadi
by: keyvanrahmadi | last post by:
Sorry if this post is rather long but hopefully you wont get bored half way through. I have a project in hand which i have started and have a deadline of 1 week. Basically what i need to do is to...
7
by: godhulirbalaka | last post by:
Hi guys, I am making a shop management program in visual basic. I want to a menu name Utiliti where Calculator, notepad, heart game, solitair game will be shown. In Utility menu I added this submenu....
0
by: .nLL | last post by:
Erorr is --------------------- Microsoft VBScript runtime error '800a0046' Permission denied /a.asp, line 3 -----------------------
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.