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 Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cannot read xml attribute

Status
Not open for further replies.

romh

Programmer
Jan 3, 2003
297
US
Hi. I am trying to read the value of an attribute, in an XML file. Its a very simple xml file.

<?xml version="1.0"?>
<product type="Tile">
<name>3ba01</name>
<pattern>rojo</pattern>
</product>

I can get both name and pattern. I cannot for the life of me, get the product type attribute. I am using c#. The following is what i am doing

///////////////////////////////////////////
xmlstr = Server.MapPath("XML/products.xml");
XmlTextReader reader = new XmlTextReader(xmlstr);
while(reader.Read())
{

if((reader.Name == "product"))
{
reader.MoveToAttribute("type");
{ reader.Attributes.Value.ToString());
}

}
if((reader.Name == "name") && (reader.NodeType == XmlNodeType.Element))
{
XML_name = reader.ReadElementString("name").ToString();

///////////////////////////////////////
etc.........
The above is just pseudocode by the way.

}

Thankyou.

What do you guys use to grab the value of an attribute?
 
I tend to read the whole document into an XMLDoc then move throught XMLNodes and use the node.Attributes["myAttrib"].Value notation.


Yet another unchecked rambling brought to you by:
Oddball
 
Code:
xmlstr = Server.MapPath("XML/products.xml");
XmlTextReader reader = new XmlTextReader(xmlstr);

XmlDocument xDoc = new XmlDocument();
xDoc.Load(reader);

// Do stuff

That should do it.



Yet another unchecked rambling brought to you by:
Oddball
 
How did you get on Romh?

Wish people would come back and tell us if they solved their problems ;)


Yet another unchecked rambling brought to you by:
Oddball
 
i haven't solved it yet because i have a little more time to fool around with it. i will complete it by monday. i will definately tell you what i did. thanks alot
 
Loading the xml file into an XmlDocument worked flawlessly. I had to add some recursion and a little more logic to separate the case where its an element, text or attribute, but it worked perfectly. Thanks alot Oddball.



private void GetChildNodes(XmlNodeList XmlNodes)
{

foreach(XmlNode node in XmlNodes)
{

switch(node.NodeType)
{
case XmlNodeType.Element:
.. etc...
break;

case (XmlNodeType.Text):
.. etc...
break;



if(node.Attributes != null)
{
foreach(XmlAttribute attrib in node.Attributes)
{
if(attrib.Name == "type")
{
X_TYPE = attrib.Value;
}
}
}

if(node.HasChildNodes)
{
GetChildNodes(node.ChildNodes);
}
}
}
 
As far as I know, XML should have a strong, predictable structure. You shouldn't need to switch on your elements, you should know which ones comes next.

If your XML document looks like this:
Code:
<product type="Tile">
   <name>3ba01</name>
   <pattern>rojo</pattern>
</product>
Then a user of the document would assume that it continues so:

Code:
<product type="Tile">
   <name>3ba01</name>
   <pattern>rojo</pattern>
</product><product type="Tile">
   <name>3ba01</name>
   <pattern>rojo</pattern>
</product><product type="Tile">
   <name>3ba01</name>
   <pattern>rojo</pattern>
</product>

This is the point where I hit a little bit of rocky ground. The way I tend to access elements is via their numeric index - but every time I put in a literal constant I feel the ghost of my (still very much alive) computer science teacher ranting on about maintainability.

Can we get some examples of how people access XML from one of these Document objects? Is it done via the indexer? Is Romh on the right track? Should we be writing schema and using some form of funky serialization?

Come on experts :) Do some experting.


Yet another unchecked rambling brought to you by:
Oddball
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top