Thread: SelectNodes (Extracting data XML File)

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    9

    SelectNodes (Extracting data XML File)

    Hi,

    I using SelectNodes to extract data from an xml file.
    Using the code below I can extract the data i.e. the prices for a particular category:

    XmlDocument *doc = new XmlDocument();

    XmlNodeList *prodNodes;

    prodNodes = doc->SelectNodes("/PRODUCTS/FRUIT[CATEGORY= 'Orange' ]/PRICE");

    However, when I try using a variable with SelectNodes it does not seem to work, therefore what is the correct syntax to use (C++).

    i.e.

    String * selectedItem = FoodCategoryLstBox->SelectedItem.ToString();


    messageNodes = prodNodes->SelectNodes("/PRODUCTS / FRUIT [CATEGORY = ‘selectedItem' ]/PRICE");

    Thanks for your help!

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    630
    What is SelectNodes?

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    More intresting is what is String
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    C++/CLI?

    If so, this isn't the forum for you, it is safe to assume that we wont be able to help you.

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    This might work.
    Code:
    String * selectedItem = FoodCategoryLstBox->SelectedItem.ToString();
    
    String * query = String::Format(S"/PRODUCTS / FRUIT [CATEGORY = ‘{0}' ]/PRICE", selectedItem);
    
    messageNodes = prodNodes->SelectNodes(query);

  6. #6
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    Hi anonytmouse,

    Thanks for your help, unfortunately I couldn’t get it to work , However, I have now used the following which works:

    XmlDocument *doc = new XmlDocument();

    XmlNodeList *prodNodes;

    String * selectedItem = FoodCategoryLstBox->SelectedItem.ToString();

    String *query = query ->Concat("/PRODUCTS /FRUIT [CATEGORY ='", selectedItem, "']/PRICE");

    prodNodes = doc ->SelectNodes(query);



    However, I use SelectNodes elsewhere in my program see below:
    ….

    String * selectedItem = FoodCategoryLstBox->SelectedItem.ToString();

    String *Item= ItemcmbBox->SelectedItem->ToString();

    String * query = query ->Concat("/PRODUCTS / FRUIT [CATEGORY ='", selectedItem, "' and ITEM= '", Item, "']/PRICE ");

    Although I cant seem to get this to work using Concat.

    I tried it using C# and I can get it working using the following syntax:

    doc.SelectNodes("/PRODUCTS/FRUIT [CATEGORY= '" + FoodCategoryLstBox.SelectedItem.ToString() + "' and ITEM = '" + ItemcmbBox.SelectedItem.ToString() + "']/ PRICE ");

    But I can’t get working under C++.

    Would anyone know what I’m doing wrong?

    Thanks in advance!

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    What is Ctring?
    in C++ you can use string class to concat substrings with +
    Code:
    string query ("/PRODUCTS / FRUIT [CATEGORY ='");
    query += selectedItem;
    query += "' and ITEM= '";
    query += Item;
    query += "']/PRICE ";
    doc.SelectNodes(query.c_str());
    something like that
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    Vart String is part of .NET as I'm using WindowsForms

  9. #9
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I'm not familar with the .Net
    But what I see in the sample looks like
    Code:
    doc.SelectNodes(String::Concat({"/PRODUCTS/FRUIT [CATEGORY= '", FoodCategoryLstBox.SelectedItem.ToString(), "' and ITEM = '" , ItemcmbBox.SelectedItem.ToString() , "']/ PRICE "}));
    Have you tried this?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  10. #10
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    error C2958: the left parenthesis '(' found at ....... was not matched correctly

    thats the error it gives me when using your code

    thanks for your help anyway vart

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Maybe you should try then
    Code:
    String^ query = String::Concat("/PRODUCTS/FRUIT [CATEGORY= '", FoodCategoryLstBox.SelectedItem.ToString(), "' and ITEM = '" , ItemcmbBox.SelectedItem.ToString() , "']/ PRICE ");
    doc.SelectNodes(query);
    I don't think it is too hard to find a correct syntax... As I see the problem is in the building the correct string, not in the SelectNodes function
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    Vart I tried but it gives me the following error:
    error C2665: 'System::String::Concat' : none of the 9 overloads can convert parameter 1 from type 'const char [30]'

    It's pretty frustrating as I know what I want it to do will work but can’t seem to get the right syntax for the string.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    maybe you should try then the old syntax S"hello"?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  14. #14
    Registered User
    Join Date
    Jan 2007
    Posts
    9
    Hi Vart thanks for all your help, sorry to be a bother could you just show me what I need to write in the Concat string section please?

    Thanks in advance for your help!

  15. #15
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    I've posted a code that was compiled with the VC 2005 Express with no errors, don't think I can find something more... As I said I never worked with the .Net
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. xor linked list
    By adramalech in forum C Programming
    Replies: 23
    Last Post: 10-14-2008, 10:13 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Writing and modifying data in a file
    By Micko in forum C Programming
    Replies: 2
    Last Post: 02-17-2005, 03:42 AM
  5. Editing a data file
    By Strait in forum C++ Programming
    Replies: 7
    Last Post: 02-05-2005, 04:21 PM