Click here to Skip to main content
15,886,026 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi guys,

I am quite basic with C# but i was wondering would anyone be able to give me some example code to the following question. XML is really confusing me.

Basicly my problem is:

I have a string string.

I want to compare the string with data i have in an XML file. the reason for this is because i want to give the user the ability to update the xml whenever they want and add new data.

so basicly, the string I have is a section of data from a file. I want to compare the data and then see which one it matches in my xml.

they are a range of articals, such as sports, games and eventually i will be expanding the list to be even longer. once i have matched them up.... say the data in the string comes from a file called hamlar.txt. i want to then display hamlar.txt in a listbox to whatever it matches up to. so say the string from hamlar.txt matches football, and is in sports. it then adds the hamlar.txt file name to a listbox. when you click the name in the list box, it displays a label saying "football"...

this will be expanded massivly... i am just keeping it simple above so i have a stepping stone and something to work from...

the xml file i have:

XML
<?xml version="1.0"?>
<References>
  <sports>
    <Ref type = "football">11 man team</Ref>
    <Ref type = "tennis">40 love</Ref>
    <Ref type = "cricket">hit a 6</Ref>
  </sports>

  <games>
    <Ref type = "monopoly">take £200</Ref>
    <Ref type = "CS">counter terrorists</Ref>
    <Ref type = "fallout">collect caps</Ref>
  </games>
</References>


obviously the file i have will be far far far far larger than this will LOADS of stuff in it.

does that make sense?

many thanks in advance.
Hyps
Posted

.Net provides a lot of nice classes for XMLs. Just google "c# xml" and you'll find a wealth of information.
 
Share this answer
 
Comments
HypeH 9-Mar-11 19:14pm    
Hi, I have done this but like i said, my knowledge of C# is quite poor. i have tried understanding some of the stuff on google. and i have tried a small tutorial on how to read an xml.

my problem is, trying to read a file and check if the string i have, contains something on a line in the xml, if it doesnt, then check the next one.. and so on. until it finds a match. then display the file name in the correct listbox e.g. if it matches football, then display the file name in a Sports listbox and a label = football when u click on it.

thank you for your fast responce though.
Sergey Alexandrovich Kryukov 9-Mar-11 20:21pm    
In all these details, you never explain why it seems to be a problem. Anyway, see my Answer.
--SA
Sergey Alexandrovich Kryukov 9-Mar-11 20:17pm    
I want to support your answer with my vote of 5. (I also recommend standard MSDN help.)
Following your advice is more useful than having a complete answer.
--SA
If the file is big and you need only a small item the best option is using System.Xml.XmlTextReader. It's the only approach (in .NET) closely following you idea "until it finds the match". It also the fastest and purely sequential, so the memory consumption does not depend on the length of input XML.

—SA
 
Share this answer
 
v2
Use DataTable. DataTable has a method to ReadXml which would read from Xml File/Stream and categorize into virtual tables. You can easily find and update the string at any point.

Note: This is a quick solution (dirty solution) but would work perfectly. ;)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900