XML and Hashtable

Last post 07-07-2008 5:12 AM by Samu Zhang - MSFT. 2 replies.

Sort Posts:

  • XML and Hashtable

    07-01-2008, 10:35 PM

    i have a small project for class where i need to read information in my XML file and put it in a hashtable.  the problem is, I've never done this before and don't have a clue where to start.  does anyone have a suggestion on how i can achieve this problem?

    this is my sample XML file:

    <books>
     <items type="horror">
        <page>100</page>
        <author>bob</author>
        <cost>50</cost>
     </items>

     <items type="comedy">
        <page>100</page>
        <author>bill</author>
        <cost>50</cost>
     </items>

     <items type="biography">
        <page>100</page>
        <author>bob</author>
        <cost>50</cost>
     </items>
    </books>

  • Re: XML and Hashtable

    07-02-2008, 12:53 AM
    • Loading...
    • bn222
    • Joined on 03-04-2008, 4:29 PM
    • Posts 76

    why not load the xml into a dataset and use the datatable for processing rather than a hashtable

    DataSet myDS = new DataSet();
    myDS.ReadXml("input.xml", XmlReadMode.ReadSchema);

    or

    Dim myDataSet as New DataSet()    
    myDataSet.ReadXml(Server.MapPath("books.xml"))

    this should give you more control in the code.

    BN
    --------------------------------------
    Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question.
  • Re: XML and Hashtable

    07-07-2008, 5:12 AM
    Answer

    Hi newbie_2007 ,

    Have a look at my sample,

     

        protected void Page_Load(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("XMLFile72.xml"));
            XmlNodeList lst = doc.SelectNodes("//items");
    
            Hashtable table = new Hashtable();
    
            for (int i = 0; i < lst.Count; i++)
            {
                Book book = new Book();
                book.type = lst[i].SelectSingleNode("@type").Value;
                book.author = lst[i].SelectSingleNode("author").InnerText;
                book.cost = lst[i].SelectSingleNode("cost").InnerText;
    
                table.Add(book.type, book);
            }
        }
      
    public class Book
    {
    	
            public string type;
            public string page;
            public string author;
            public string cost;
    	
    }

     

     

    Sincerely,
    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Page 1 of 1 (3 items)
Microsoft Communities
Page view counter