Reputation: 21
I made an XML file using linq.(The user fills some textboxes and on submit the data is being added to the xml in this format :
<Departments>
<Department1>
<Course1 code="x">
<CourseName></CourseName>
<CourseCode></CourseCode>
</Course1>
<Course2 code="y">
<CourseName></CourseName>
<CourseCode></CourseCode>
</Course2>
</Department1>
<Department2>
<Course3 code="a">
<CourseName></CourseName>
<CourseCode></CourseCode>
</Course3>
</Department2>
</Departments>
Now I want to load the xml to another form and display the data I have in the Xml. If I have 1 course in department one when I load my form 2textbox should appear(one with the code, one with the name), If I have 2 courses in department1 when I load the form there will be 2 textboxes and 2 other beneath them for the second course(name and code). But I am not sure since I haven't used c# and xml/ling until recently.
The steps would be I think :
For the form labels/textboxes I thought of using ArrayList
for( int i = 0 ; i = how many courses I have in Departments1 in the xml file)
{
coursecode.Add(new TextBox());
course.Add(new TextBox());
System.Drawing.Point p = new System.Drawing.Point(150, 100 + i * 30);
(coursecode[i] as TextBox).Location = p;
(course[i] as TextBox).Location = p;
(course[i] as TextBox).Size = new System.Drawing.Size(70, 20);
(course[i] as TextBox).Size = new System.Drawing.Size(70, 20);
this.Controls.Add(coursecode[i] as TextBox);
this.Controls.Add(coursename[i] as TextBox);
}
But I dont know how to make a linq query and like I said above assign its value to a textboxt or label.
Update: Managed to display it. Thanks for the help.
Upvotes: 0
Views: 172
Reputation: 725
Would something like this work? It doesn't use a linq query, but it does get the courses into a List<>
XDocument doc = XDocument.Load("YourXmlFileNameHere.xml");
List<XElement> CoursesInDepartment1;
// Step 1
CoursesInDepartment1 = doc.Element("Departments").Element("Department1").Elements().ToList();
// Step 2
foreach (XElement elem in CoursesInDepartment1) {
TextBox nameBox = new TextBox();
TextBox codeBox = new TextBox();
//Set the location, size, ect.
//Step 3
nameBox.Text = elem.Element("CourseName").Value;
codeBox.Text = elem.Element("CourseCode").Value;
this.Controls.Add(nameBox.Text);
this.Controls.Add(codeBox.Text);
}
Console.ReadLine();
Hope this helps!
Upvotes: 0
Reputation: 495
As Mzn said, the standard documentation should be the place to look. A few methods that you will be using will be Elements
or Descendants
, Count
, and getting the Value
property for the rest.
A word of caution, since you did not specify in the xml snippet how the data is stored for <CourseName>
and <CourseCode>
I assume the worse which is that the values you want are not stored as attributes in which case the Value
property will return all values from that node as well as it's children. If they are attributes, it will be easier as you can use the Attribute
method to get a string
for the data.
Once you get the data out, then it's simply create the textboxes, position them, and insert the data into their respective Text
properties.
A tip: use a List<T>
instead of an ArrayList
. The List<T>
is type safe, so errors from failed casting are eliminated. Also, I found using "dot notation" for LINQ as a good start when learning it as it follows the same conventions as vanilla c# code.
Upvotes: 1