Hello community im new! I am really rusty at C# but i do not want to learn the basics all over again so i hit the ground running and ran into a hurdle. I want to use an xml file for a database. Its a car repair tracking system i am working on so this is not the initial file buy you'll get the idea:

<cars>
	<car Id="1">
	<name>Mr Thompson</name>
	<model>Acura</model>
	<address>Shelbyville</address>
	<status>Ready for pickup</status>
	</car>

	<car Id="2">
	<name>Mr Simpson</name>
	<model>Ford</model>
	<address>Springfield</address>
	<status>in progress</status>
	</car>
</cars>

What i want is the C# asp.net application to pick up the xml nodes by the car id. However I do not know how to pick up the value of the id through XmlReader (maybe one of you know a better method, but im working with this for now until i find something better.) I have made a program however it only picks up the first set of tags, the Mr Thompson ones, What i want to know is how do i set it so it can pick up the first set when I enter id=1 and pick up the second set when i enter id=2. here is the code i am working with:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    string name;
    string model;
    string address;
    protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("hello world");


    }
    protected void Button1_Click1(object sender, EventArgs e)
    {
        XmlTextReader xmlReader = new XmlTextReader(Server.MapPath("vehix.xml"));

        int intId = 1;
        //   int intCar = 0;


        while (xmlReader.Read())
        {
            if (xmlReader.Name.ToString() == "car")
            {
                if (xmlReader.HasAttributes)
                {
                    while (xmlReader.MoveToNextAttribute())
                    {
//                        if (xmlReader.Name == "Id")
//                        {
//                           intId = 1;
                                //Int32.Parse(xmlReader.Value);


//                        }
                    }
                }
            }
            if (xmlReader.Name.ToString() == "name") name = xmlReader.ReadString().Trim();
            if (xmlReader.Name.ToString() == "model") model = xmlReader.ReadString().Trim();
            if (xmlReader.Name.ToString() == "address") address = xmlReader.ReadString().Trim();

        }
        xmlReader.Close();
        TextBox1.Text = name;
        TextBox2.Text = model;
        TextBox3.Text = address;
    }
}

so basically all i want to know is how to pick up the id number of the xml tag id, i can incorporate the rest myself or until i run into more problems and come back.

thank you

Recommended Answers

All 2 Replies

Use Xml LINQ.

using System;
using System.Xml.Linq;

class Test
{
 static void Main()
  {
   string path = @"c:\path1\sample.xml";
   XDocument doc = XDocument.Load(path);
   foreach (var t in doc.Descendants("car"))
      {
          Console.WriteLine(t.Attribute("Id").Value );
      }
  }
}
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.