Hi guys, My C# programming is quite basic, and i would like to note, i have tried a few tutorials on reading XML and creating my program. i have done as much as i can understand so far, and so i am hoping someone could maybe explain to me how to go about doing this part.

my program is quite more complicated than this but i am only after the basics, so i can then progress on it my self, i want to try and learn this as best i can :) i have tried using MSDN and other sites but its just bouncing in and out of my head.

Ok:

1)I am reading a file and storing the data into a string.

2)I have an XML file (atm with only a few things but will be expanded!) the XML file contains the below data:

<?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>

now, as you can see from the XML File, I have Sports and Games. Eventually this will be expanded to a wider ranger of things.
in each section i have stuff relating to these, such as for sports, i have football, tennis, cricket.
each one of these has some text.

my program has 2 listboxes at the moment, 1 for Sports and 1 for Games.

What i want to do:

I want to take the string that I took from a file, then check the XML file. for example, if the string i have says "each team in football has an 11 man team" it will check through each line in the XML and see that it contains "11 man team" which is for football. it then takes the file name, and lists it into the Sports listbox. when the user clicks on the file name (e.g. bubb.txt) it will display in a text box "Football".

This is very basic compared to what i want it to do, its more of a starting point that i feel i can work with and expand on in a much more complex way.

i appreciate any help you guys may be able to provide me.

Regards,
Hypeh

hi guys, just wondered if anyone had any ideas on this, i am still stuck on what to do. many thanks in advance. Hypeh

anyone able to help? :(

I worked out this, but this is perhaps not what you want.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            XElement Refs = XElement.Parse(
              @"<References>
                    <sport>  
                        <type>football</type>
                        <Info>11 man team</Info>
                    </sport>
                    <sport>  
                        <type>tennis</type>
                        <Info>40 love</Info>
                    </sport>
                    <sport>  
                        <type>cricket</type>
                        <Info>hit a 6</Info>
                    </sport>                                 
                </References>");
             //<games>
             //       <Ref type = monopoly>take £200</Ref>
             //       <Ref type = CS>counter terrorists</Ref>
             //       <Ref type = fallout>collect caps</Ref>
             //     </games>
            var sportinfo =
                from sport in Refs.Elements("sport")
                where (string)sport.Element("type") == "tennis"
                select sport.Element("Info");
            foreach (var Inf in sportinfo)
               Console.WriteLine(Inf.Value);
            Console.ReadKey();
        }
    }
}

ah thanks for the reply :)

Im afraid thats not really what i want. I don't want the XML data in the Code.

i need an XML document that contains those things. and at a later date, i will modify the XML so at the moment it has "Sports" and "Games" and they only have a few things, like "sports" currently has "football" "tennis" "cricket" but at a later date i may add more and then it may contains "golf" "hockey" "baseball". so the purpose of the XML is so the user can change or update it at anytime they want.

basicly, I want to have the program read in a file, then check if the XML contains any of the text from the file. if it does, then display that file name in a list box related to that category e.g. Sports listbox contains all the file names which contain stuff like golf, hockey, tennis etc etc.

basicly at the moment the program is set up for testing and compares a stored string (taken from a file thats read in) and is compared to a string i have hard coded which is like "tennis = "40 love". so the program checks if the first string taken from the file contains "40 love" it says "yes this matches, and moves it to a sports listbox".

how can i change it so instead of looking at the "tennis = "40 love"... it looks at the XML file. it reads the xml file and searches through. if it matches tennis in the xml, it sees that tennis is in the sports category and then puts the file name into the sports listbox... then goes to the next file...

many thanks,

Hypeh

I was just simulating file acces with my string. In that place in my code there should be a XML read operation or something.

thats my biggest problem i think. i really dont understand how to read in my XML file. and retrieve data from it . or compare the data in it to the stored string i have.

i dunno what it is, i just cant seem to get my head around it. iv spent hours looking at examples and msdn and it just seems so much more complicated than what i need.

Did you try to read this tutorial?
Looks not so difficult to follow I think.
Success!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace TestBed {
    class Program {
        static void Main(string[] args) {
            XDocument myDoc = XDocument.Load("myxml.xml");

            IEnumerable<String> result = from References in myDoc.Elements()
                                         from Sports in References.Elements()
                                         from Refs in Sports.Elements()
                                         where Refs.Attribute("type").Value == "football"
                                         select Refs.Value;

            foreach (String r in result) {
                Console.WriteLine(r);
            }
            Console.ReadLine();
        }
    }
}

Hi,

thank you for your reply.

it seems to be getting me toward what im sort of looking for i think. but a little bit around the wrong way.

i have taken ur code and changed it a little to try and get it doing what i need but it doesnt work. maybe from looking at how i changed it you may understand?

basicly, i have put "11 man team" already there. so it is checking the XML document. when it finds the value "11 man team" it returns the Type.

does that make sense?

so maybe 1 output that says "11 man team" matches "type"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
 
namespace TestBed {
    class Program {
        static void Main(string[] args) {
            XDocument myDoc = XDocument.Load("myxml.xml");
 
            IEnumerable<String> result = from References in myDoc.Elements()
                                         where Refs.Value == "11 man team";
                                         select sigs.Attribute("type").value;
            foreach (String r in result) {
                Console.WriteLine(r);
            }
            Console.ReadLine();
        }
    }

I got an exception when loading the xml file with the pound sign, I removed it and the code of Momerath works fine.
Just wondering why still in the sports department, it also works for monopoly...

static void Main(string[] args)
        {
            //Contents of xmltest file, notice the missing pound sign
            // in the monopoly line

            //<?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>

            XDocument myDoc = XDocument.Load(@"c:\Users\Marivoet\Documents\xmltest.xml");
            IEnumerable<String> result = from References in myDoc.Elements() 
                                         from Sports in References.Elements() 
                                         from Refs in Sports.Elements() 
                                         where Refs.Attribute("type").Value == "monopoly" 
                                         select Refs.Value;
            foreach (String r in result) 
            {                
                Console.WriteLine(r);
            } 
            Console.ReadLine();  
        }

This gves the correct result take 200

its doing it the oposite to what i want though.

this code is looking at monopoly and returning "take 200"

however, I want it to look at "take 200" compare it to the string I already have... say the string i got is called "StoredValue".

so basicly i got:

StoredValue = the player can take 200.

I want it to check the XML, find "Take 200" because its in that string.

then return the value "Monopoly" and say a match was found. if a match wasnt found then say no match found.

thanks,
Hypeh

Not tested, but I believe you are looking for something like:

IEnumerable<String> result = from References in myDoc.Elements()
                                         from Sports in References.Elements()
                                         from Refs in Sports.Elements()
                                         where Refs.Value.Contains("take 200")
                                         select Refs.Attribute("type").Value;

ooooh getting there. very close.

instead of having the XML contain the string.

the string i will have may be "the player may take 200"

as you can see the XML contains "take 200"

so i need to check if the String Contains the XML value

:)

Replace the "take 200" with a variable holding the string you want to check.

Nope, it doesnt work because its the opposite way around. the string has the most text. the XML file only has "take 200".

I need it so its:

if the string contains the XML value

at the moment it is:

if the XML value contains the string value

thanks,
Hypeh

Reverse the compares then.

you mean, put the longer text in the XML?

i cant do that, because I will never know what the text is.

the string I have comes from files that are taken from a directory, and are always different.

some might say "take 200 if you pass go" and another may say "when you pass go take 200".

i need to make it, so it checks if the string contains the XML.

ahhhh i got there:

i replaced:

where refs.Value.Contains(storedValue)

with:

where (storedValue.Contains(refs.Value))

and it worked :)

That's what I mean :) Now you've learned something new today and can be lazy for the rest of Saturday.

hehe thanks a lot mate. this is just a tiny bit of the work to do :) now i understand this. theres a lot of work to do. thank you very much for the help so far. if i get stuck further i'll come and poke! haha

cheers :)

just curious,

in stead of outputting this to console. how would i output it to a listbox?

am i able to get it to output the "take 200" as well?

so in a text box it shows "take 200"

and in a listbox it displays "monopoloy"

is that possible?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.