How to search an XML File

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MZimmerman6
    New Member
    • May 2010
    • 8

    How to search an XML File

    I have a question about how to read through an XML Document

    Here is an example of a similar XML to what I am using

    Code:
    <People>
        <Person id = "n">
             <LastName>Norris</LastName>
             <FirstName>Chuck</FirstName>
             <Hobby>badassery</Hobby>
        </Person>
        <Person id = "b">
             <LastName>Bush</LastName>
             <FirstName>George</FirstName>
             <Hobby>Dumbassery</Hobby>
        </Person>
    </People>
    where the id corresponds with the first letter of their last name.

    I want to be able to search through an xml and match it up to a name, and if the last name I am searching matches the first letter, which is the attribute, to look at the child nodes, to see if the full last name matches.

    so basically if I were to search Bush, it would completely skip Chuck Norris (which is probably not possible due to the physical prowess but anyway), and go to the bush entry, see the attribute matches the first character of the name being searched, and then look through the child nodes, and then save them as strings to an array or something.

    I tried messing with XmlTextReader and its components but can not really figure out what will do what I want and how to get it to not look farther into each element and its corresponding nodes.

    Thanks in advance for your help!
  • MZimmerman6
    New Member
    • May 2010
    • 8

    #2
    Nevermind, I got it to work by instead of saving each thing as a child node, I just saved them as attributes to the element. and the name of the element being the person's last name. so the xml will look like this

    Code:
    <People>
       <Norris first ="Chuck" hobby = "badAssery"/>
       <Bush first ="George" hobby = "dumbAssery">
    </People>
    first off it saves a good amount of characters thus reducing size and there is a getAttribute method that makes it easy as long as you know the names of the attributes, which I do.

    Code:
    public List<List<String>> getMatches(String xmlPath, String lastName, String firstName) {
       FileStream fs = new FileStream(xmlPath,FileMode.Open);
       XmlDocument xmlDoc = new XmlDocument();
       doc.Load(fs);
       XmlNodeList matches = xmlDoc.SelectNoes("People/" + lastName)
       List<List<String>> Names = new List<List<String>>();
       List<String> info = new List<String>();
       foreach (XmlElement match in matches) {
          if (firstName.toLower().Equals(match.getAttribute("first").toString().toLower()) {
             info = new List<String>();
             info.Add(lastName)
             info.Add(match.getAttribute("first").toString());
             info.Add(match.getAttribute("hobby").toString());
             names.Add(info)
          }
       }
       return Names;
    }

    Comment

    Working...