Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4265428
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:37:21+00:00 2026-05-21T06:37:21+00:00

Here is my XML file <?xml version=1.0 encoding=utf-8 ?> <storage> <Save Name =Lifeline> <Seconds>12</Seconds>

  • 0

Here is my XML file

<?xml version="1.0" encoding="utf-8" ?> 
<storage>   
<Save Name ="Lifeline">
 <Seconds>12</Seconds>
 <Minutes>24</Minutes>
 <Hours>9</Hours>
 <Days>25</Days>
 <Months>8</Months>
 <Years>2010</Years>
 <Health>90</Health>
 <Mood>100</Mood>  
</Save> 

<Save Name ="Hellcode">   
 <Seconds>24</Seconds>
 <Minutes>48</Minutes> 
 <Hours>18</Hours>
 <Days>15</Days>
 <Months>4</Months>
 <Years>1995</Years>
 <Health>50</Health>
 <Mood>50</Mood>  
</Save> 

Here is a code which get’s data from XML and loads it into application.

System.IO.StreamReader sr = new System.IO.StreamReader(@"Saves.xml");

System.Xml.XmlTextReader xr = new System.Xml.XmlTextReader(sr);

System.Xml.XmlDocument save = new System.Xml.XmlDocument();

save.Load(xr);


XmlNodeList saveItems = save.SelectNodes("Storage/Save");

XmlNode seconds = saveItems.Item(0).SelectSingleNode("Seconds");
sec = Int32.Parse(seconds.InnerText);

XmlNode minutes = saveItems.Item(0).SelectSingleNode("Minutes");
min = Int32.Parse(minutes.InnerText);

XmlNode hours = saveItems.Item(0).SelectSingleNode("Hours");
hour = Int32.Parse(hours.InnerText);

XmlNode days = saveItems.Item(0).SelectSingleNode("Days");
day = Int32.Parse(days.InnerText);

XmlNode months = saveItems.Item(0).SelectSingleNode("Months");
month = Int32.Parse(months.InnerText);

XmlNode years = saveItems.Item(0).SelectSingleNode("Years");
year = Int32.Parse(years.InnerText);

XmlNode health_ = saveItems.Item(0).SelectSingleNode("Health");
health = Int32.Parse(health_.InnerText);

XmlNode mood_ = saveItems.Item(0).SelectSingleNode("Mood");
mood = Int32.Parse(mood_.InnerText);

The problem is that this code loads data inly from “Lifeline” node. I would like to use a listbox and be able to choose from which node to load data.

I’ve tried to take string from listbox item content and then use such a line

XmlNodeList saveItems = save.SelectNodes(string.Format("storage/Save[@Name = '{0}']", name)); 

variable “name” is a string from listboxe’s item. While compiled this code gives exception.
Do somebody knows a way how to select by attribute and load nedeed data from that XML?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T06:37:21+00:00Added an answer on May 21, 2026 at 6:37 am

    Since it is not that much data, I’d suggest loading all information to a list of saves(constructor) and then drawing from there which one the user would like to use…

    As for things not working, I personally use a lower level approach to get my data and it is not error prone. Remodeling it to fit your problem a bit:

    int saves = 0;
    
    List<Saves> saveGames = new List<Saves>();
    
    saveGames.Add(new Saves());
    
    while (textReader.Read())
    {
       if (textReader.NodeType == XmlNodeType.Element)
          whatsNext = textReader.Name;
       else if (textReader.NodeType == XmlNodeType.Text)
       {
          if (whatsNext == "name")
             saveGames[saves].name = Convert.ToString(textReader.Value);
          //else if statements for the rest of your attributes
          else if (whatsNext == "Save")
          {
             saveGames.Add(new Saves());
             saves++;
          }
       }
       else if (textReader.NodeType == XmlNodeType.EndElement)
          whatsNext = "";
    }
    

    Basically throw everything in the xml file into a list of objects and manipulate that list to fill the listbox. Instead of having Saves name = “…”, have a name attribute as the first attribute in the save.

    Code tags hate me. Why they break so easily ( ._.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's an example of an XML file created in InfoPath: <?xml version=1.0 encoding=UTF-8?> <?mso-infoPathSolution
I have an xml file which looks like this <?xml version=1.0 encoding=UTF-8 ?> <BulkDataExchangeRequests
I have a xml file which has the following header: <?xml version=1.1 encoding=UTF-8?> .
Here is my ultimate goal... to take this xml file.. <?xml version=1.0?> <Songs> <Song>
I'm trying to parse UTF-8 XML file and save some parts of it to
I have a XML file. Here is a small version of that. <?xml version=1.0
here is the XML file : <com.google.android.maps.MapView android:layout_width=fill_parent android:layout_height=fill_parent android:apiKey=0q7NUYm4bgzeXlqXtKYVPJDRWUJmt8Cu0gvbWMx android:id=@+id/map_view /> and Manifest
Here is part 1 of our problem (Loading a dynamically generated XML file as
Here is my first attempt at validating XML with XSD. The XML file to
I have an XML file that is very long, but here is a shot

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.