Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding XML Tokens or Attributes

Status
Not open for further replies.

RileyCat

Programmer
Apr 5, 2004
124
US
Does anybody have a quick and dirty method for identifying the tokens that are defined in a given attribute within an XML item?

For example, given the following . . .

Code:
<configuration/>
<prod/>
<emailaddr to="somename@someurl.com" from="somename@someurl.com">

With this, I want to be able to stick in a list box for the user to select either the "to" or "from" token to view the value of that token.

I can get to the attribute name itself . . . i.e. configuration, prod, or emailaddr. But I am specifically trying to get to the tokens within an attribute.

Thanks kindly!



Stay Cool Ya'll! [smile2]

-- Kristin
 
Kristin,

Your problem description might itself be causing some confusion. You should probably adopt the following terminology.

Those things you are calling attribute are called elements.

The things you are calling tokens are called attributes.

Now, armed with this knowledge, I can assure you that XPath can refer to all of the attributes within an element, even if you do not necessarily know the names of the attributes. Are you using XSLT?

Tom Morrison
 
>I can get to the attribute name itself . . . i.e. configuration, prod, or emailaddr

Then you simply select the attribute node in the context of the "element" emailaddr, like these.
[tt] <xsl:value-of select="@to">
<xsl:value-of select="@from">
[/tt]

 
Forgotten to close them!
[tt] <xsl:value-of select="@to" />
<xsl:value-of select="@from" />
[/tt]
 
Thank you for the tips thus far.

Actually, I am trying to do this using C#.

Any more ideas?


Stay Cool Ya'll! [smile2]

-- Kristin
 
I have found it works most efficiently in C# with the following code . . .

Code:
_data.Seek(0,0) ;
XmlTextReader reader = new XmlTextReader (_data) ; 
while (reader.Read() != false)
{
   switch (reader.NodeType)
   {	
	case System.Xml.XmlNodeType.Element:
 	   if (reader.Name.Equals(Element))
  	   {
		int AttrCount = reader.AttributeCount ;
		for (int i = 0 ; i < AttrCount ; i++)
		{
                   reader.MoveToAttribute(i) ;
                   this.lstAttributes.Items.Add
                       Convert.ToString(reader.Name)) ;
		}
	   }
	   break ;
	}
    }
}

In case anybody is interested . . .

Stay Cool Ya'll! [smile2]

-- Kristin
 
Easier to use XPath:
Code:
XmlDocument xml = new XmlDocument();
xml.Load("test.xml");
XmlNodeList attributes = xml.SelectNodes("//@*");
foreach(XmlNode attribute in attributes)
{
    //Do stuff with each attribute
}

Jon

"I don't regret this, but I both rue and lament it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top