Simple Question, but need it quickly. My server application receives an xml string which I am trying
to read using XmlTextReader. I am having a problem getting it to recognize an attribute and retrieving the data.
This is the latest I have tried:
It seems when I step through the debugger, it does not seem to recognize that it even has an attribute associated with
an element. What do I need to do?
XML String:
<?xml version="1.0" encoding= "utf-8" ?>
<Message>
<Name>Name</Name>
<DateTime>1/24/2005 :00:23PM</DateTime>
<Machine>MachineName</Mach
ine>
<MessageNumber>999</Messag
eNumber>
<Detail Type = "Detail1">Text1</Detail>
<Detail Type = "Detail2">Text2</Detail>
</Message>
To Read The Data:
XmlTextReader reader = new XmlTextReader(sXmlString);
while (reader.Read())
{
if (reader.NodeType.Equals(Xm
lNodeType.
Element))
{
sNode = reader.Name;
sValue = reader.Value;
switch (sNode)
{
case "Name":
case "DateTime":
case "MachineName" :
case "MessageNumber" :
//do stuff
break;
case "Detail":
if (reader.HasAttributes) // This fails every time
sattribute = reader.GetAttribute("Type"
);
//I've also tried this
while (reader.MoveToNextAttribut
e()) // This also failed
{
if (reader.Name.Equals("Type"
))
m_DetailList.Add(reader.Va
lue);
}
reader.MoveToElement();
//And Tried This
else if (reader.NodeType.Equals(Xm
lNodeType.
Attribute)
) //failed
{
string svalue;
string snode;
svalue = reader.Value;
snode = reader.Name;
System.Diagnostics.EventLo
g.WriteEnt
ry(this.To
String(),
"DetailAttrName: " + reader.Name);
System.Diagnostics.EventLo
g.WriteEnt
ry(this.To
String(),
"DetailText: " + reader.Value);
}
}
Start Free Trial