-
xml Q
Hi. I hope someone can help me.
I am writing a test code, just a small project, to read and write to xml. I can read and write.
Q is, how do i perform a search based on a particular node? once that node has been found, I wish to then show all the details of the entities within that node.
I am currently using a switch statement.
Thing is, it kinda is like a database (SQL is not an option in this case). Like SQL, you can search for all products in a table on a particular field with a certain value and SQL will retrieve those items with the search criteria.
This is kind of what I would like to do.
So lets say I have an xml (table name lets say will be "computers").
xml will have these entities:
computer (each node)
and in each computer node there will be these entities and each entity will have some value:
<computer>
<computerID />
<computerName />
<computerDescription />
<computerRAM />
<computerHDD />
<computerGraphics />
</computer>
etc...
Now, what I want is that if a user types in that they wish to search for computerID number 2, the search engine will go through each computer node and if the computer node entity "computerID" is == to the user input then it will display all the information on that computer node
I also want this search routine to show me all the computer node entity values if the user says that they wish to search for all computers based on the computerRAM with the value of "512" - all of the computer entities should return me with entities which contain "512" in the computer nodes
am I making it confusing? Please let me know.
current code:
Code:
private void DoSearchSpecific(string nodeName, string text)
{
XmlTextReader xtr = new XmlTextReader("xmlWriteTest.xml");
while(xtr.Read())
{
switch(xtr.NodeType)
{
case XmlNodeType.Element:
if (xtr.Name.Equals(nodeName))
{
MessageBox.Show("node " + nodeName + " found!");
}
break;
case XmlNodeType.Text:
MessageBox.Show("Value for this node: " + xtr.Value);
break;
}
}
xtr.Close();
}
-
Re: xml Q
updated code:
Code:
private void DoSearchSpecific(string fieldName, string text)
{
XmlTextReader xtr = new XmlTextReader("xmlWriteTest.xml");
while(xtr.Read())
{
if(xtr.NodeType == XmlNodeType.Element)
{
if (xtr.LocalName.Equals(fieldName))
{
string currentFieldValue = xtr.ReadElementString(fieldName);
if (currentFieldValue.Equals(text))
{
MessageBox.Show(currentFieldValue);
}
//need to display all information in this element
}
}
}
xtr.Close();
}
well I am almost there. I just need it now so that for each Parent element (after if (xtr.LocalName.equals())) that there is, I want to get ALL the sub/child elements and it's values.
how?