XML Nodes Iteration Question

L

Lee

Hello,

I am having some problem iterating over nodes. I keep getting the same
value using SelectSingleNode even though the nodes does not have that
value:

XML Document:

<?xml version="1.0" encoding="utf-8" ?>
<Class>
<ClassName>testItem</ClassName>
<DBTableName>Servers</DBTableName>
<IDField>
<ClassFieldName>ServerID</ClassFieldName>
<DBFieldName>ServerID</DBFieldName>
<DBFieldType>5</DBFieldType>
<AutoGenerated>False</AutoGenerated>
<GeneratorName>None</GeneratorName>
</IDField>
<Fields>
<Field>
<ClassFieldName>ServerFirst</ClassFieldName>
<DBFieldName>ServerFirst</DBFieldName>
<DBFieldType>9</DBFieldType>
</Field>
<Field>
<ClassFieldName>ServerLast</ClassFieldName>
<DBFieldName>ServerLast</DBFieldName>
<DBFieldType>9</DBFieldType>
</Field>
<Field>
<ClassFieldName>PositionName</ClassFieldName>
<DBFieldName>PositionName</DBFieldName>
<DBFieldType>9</DBFieldType>
</Field>
</Fields>

</Class>


In my code I have:

XmlNode node = Doc.SelectSingleNode("//Class//Fields");
foreach (XmlNode nd in node)
{
DTField field = new DTField(nd);
_Fields.Add(field);
}

The constructor for the DTField object is as follows:

public DTField(XmlNode fieldNode)
{
_ClassFieldName = fieldNode.SelectSingleNode
("//Field//ClassFieldName").InnerText;
_DBFieldName =
fieldNode.SelectSingleNode("//Field//DBFieldName").InnerText;
int fType =
int.Parse(fieldNode.SelectSingleNode("//Field//DBFieldType").InnerText);
_DBFieldType = (DTDBFieldType)fType;
}

Now, I have put breakpoints all over the see what the values are when
they get called, etc. There are 3 Field nodes in the xml doc, but if I
put a break point in the DTField constructor the value assigned to
_ClassFieldName is *always* "ServerFirst", the value for the first node.

Wierd thing is that intellisense shows the real values as they should
be when I hover over the fieldNode object during debug.


--
Warm Regards,
Lee

"Upon further investigation it appears that your software is missing
just one thing. It definitely needs more cow bell..."
 
L

Lee

Wow, this is really frustrating. I've done a few little projects in
VB.net that works with XMLDocument and I've never had this problem.

I've been tearing my hair out trying to figure out what's wrong and I'm
of ideas. I have even broken it down to a simple test:

Given the following XML:

<?xml version="1.0" encoding="utf-8" ?>
<Class>
<ClassName>testItem</ClassName>
<DBTableName>Servers</DBTableName>
<IDField>
<ClassFieldName>ServerID</ClassFieldName>
<DBFieldName>ServerID</DBFieldName>
<DBFieldType>5</DBFieldType>
<AutoGenerated>False</AutoGenerated>
<GeneratorName>None</GeneratorName>
</IDField>
<Fields>
<Field>
<ClassFieldName>ServerFirst</ClassFieldName>
<DBFieldName>ServerFirst</DBFieldName>
<DBFieldType>9</DBFieldType>
</Field>
<Field>
<ClassFieldName>ServerLast</ClassFieldName>
<DBFieldName>ServerLast</DBFieldName>
<DBFieldType>9</DBFieldType>
</Field>
<Field>
<ClassFieldName>PositionName</ClassFieldName>
<DBFieldName>PositionName</DBFieldName>
<DBFieldType>9</DBFieldType>
</Field>
</Fields>
</Class>


....and the following code:

private void button2_Click(object sender, EventArgs e)
{
XmlDocument Doc = new XmlDocument();
Doc.Load(Environment.CurrentDirectory +
"\\DataMappings\\testItem.xml");
XmlNode node = Doc.SelectSingleNode("//Class//Fields");
foreach (XmlNode nd in node)
{
textBox1.AppendText(Environment.NewLine +
nd.SelectSingleNode("//Field/ClassFieldName").InnerText);
}
}


....What is the output the following?

ServerFirst
ServerFirst
ServerFirst

It should be:

ServerFirst
ServerLast
ServerPosition

Any help would be appreciated. :)



--
Warm Regards,
Lee

"Upon further investigation it appears that your software is missing
just one thing. It definitely needs more cow bell..."
 
L

Lee

Hmmm. I think I have it. Need to use relative path with Xpath to get
the values that I want.


--
Warm Regards,
Lee

"Upon further investigation it appears that your software is missing
just one thing. It definitely needs more cow bell..."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top