I have an xml File set out as below, how do i use c# to read just selected nodes, for example, i only want to read the Title and End nodes. I am using ASp.net and c# for my web project.
I have the following code so far but it doesnt work, it reads the root as project but thats it. It goes through the foreach first line then exits at the bottom of the function
XmlDocument doc = new XmlDocument();
doc.Load("C:\\Users\\daniel\\Documents\\uni work\\Final year\\FYP\\Project plan\\Project Plan 2012.xml");
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("//Task");
foreach (XmlNode node in nodes)
{
string task1 = node["Task"].InnerText;
foreach (XmlNode n in node)
{
string name = n["name"].InnerText;
TextBox1.Text = name;
}
}
As the xml file as about 90 rows i dont need to view all, is there a way to start at a specifc point. So for example if i have 40 tasks, can i start at tasks 23?
Sure! Just use a for loop instead of a foreach loop:
//Start at the 23rd Task and Iterate through the rest
for(int i = 22; i < nodes.Count; i++)
{
string name = nodes[i]["Title"].InnerText;
string id = nodes[i]["ID"].InnerText;
string start = nodes[i]["Start"].InnerText;
string end = nodes[i]["End"].InnerText;
}
Member
22 Points
75 Posts
Reading an XML file
Jan 23, 2013 10:11 AM|annoscia|LINK
Hi All,
I have an xml File set out as below, how do i use c# to read just selected nodes, for example, i only want to read the Title and End nodes. I am using ASp.net and c# for my web project.
I have the following code so far but it doesnt work, it reads the root as project but thats it. It goes through the foreach first line then exits at the bottom of the function
All-Star
114593 Points
18503 Posts
MVP
Re: Reading an XML file
Jan 23, 2013 10:53 AM|Rion Williams|LINK
Based on your XML you are attempting to target the wrong node. You are using "Task" when you should be using "task" :
Check out the example below, which demonstrates how to iterate through all of your "task" nodes and grab the properties :
Member
22 Points
75 Posts
Re: Reading an XML file
Jan 23, 2013 11:04 AM|annoscia|LINK
Hi,
As the xml file as about 90 rows i dont need to view all, is there a way to start at a specifc point. So for example if i have 40 tasks, can i start at tasks 23?
All-Star
114593 Points
18503 Posts
MVP
Re: Reading an XML file
Jan 23, 2013 11:12 AM|Rion Williams|LINK
Sure! Just use a for loop instead of a foreach loop:
Contributor
4392 Points
933 Posts
Re: Reading an XML file
Jan 23, 2013 11:14 AM|Vipindas|LINK
Try this
Hope this helps...
Member
22 Points
75 Posts
Re: Reading an XML file
Jan 23, 2013 11:30 AM|annoscia|LINK
Hi Rion,
I use the example code given by yourself but run into a problrm on
I receive the following error:
Have you got an idea about what this could mean?
EDIT:
ignore me haha long day programming, instead on Title it should be Name! Sorry
But many thanks for all of your help
All-Star
114593 Points
18503 Posts
MVP
Re: Reading an XML file
Jan 23, 2013 11:34 AM|Rion Williams|LINK
No problem! I'm glad you got everything to work out :)
All-Star
94130 Points
18109 Posts
Re: Reading an XML file
Jan 24, 2013 08:47 PM|Decker Dong - MSFT|LINK
Hi,
Rion Williams's anwer I cannot re-produce your issue, either.
So I'll mark his as answer.
Anything urgent, please feel free to feedback.