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;
}
annoscia
Member
22 Points
75 Posts
Reading an XML file
Jan 23, 2013 02:11 PM|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.
<?xml version="1.0" encoding="utf-8" ?> <project> <task> <ID>1</ID> <Title>Task 1</Title> <Start>04/01/2013</Start> <End>07/01/2013</End> </task> </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; } }Rion William...
All-Star
27896 Points
4618 Posts
Re: Reading an XML file
Jan 23, 2013 02:53 PM|LINK
Based on your XML you are attempting to target the wrong node. You are using "Task" when you should be using "task" :
//XmlNodeList nodes = root.SelectNodes("//Task"); XmlNodeList nodes = root.SelectNodes("//task"); //Use thisCheck out the example below, which demonstrates how to iterate through all of your "task" nodes and grab the properties :
XmlDocument doc = new XmlDocument(); doc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8"" ?><project><task><ID>1</ID><Title>Task 1</Title><Start>04/01/2013</Start><End>07/01/2013</End></task></project>"); XmlElement root = doc.DocumentElement; XmlNodeList nodes = root.SelectNodes("//task"); foreach (XmlNode node in nodes) { string name = node["Title"].InnerText; string id = node["ID"].InnerText; string start = node["Start"].InnerText; string end = node["End"].InnerText; //Perform additional operations here }annoscia
Member
22 Points
75 Posts
Re: Reading an XML file
Jan 23, 2013 03:04 PM|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?
Rion William...
All-Star
27896 Points
4618 Posts
Re: Reading an XML file
Jan 23, 2013 03:12 PM|LINK
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; }Vipindas
Contributor
5514 Points
810 Posts
Re: Reading an XML file
Jan 23, 2013 03:14 PM|LINK
Try this
XmlDocument doc = new XmlDocument(); doc.Load("C:\\Users\\daniel\\Documents\\uni work\\Final year\\FYP\\Project plan\\Project Plan 2012.xml"); XmlNodeList nodes = doc.GetElementsByTagName("task"); foreach (XmlNode node in nodes) { XmlElement elem = (XmlElement)node; string ctry = elem.GetElementsByTagName("Start")[0].InnerText; Console.WriteLine(ctry); }Hope this helps...
annoscia
Member
22 Points
75 Posts
Re: Reading an XML file
Jan 23, 2013 03:30 PM|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
Rion William...
All-Star
27896 Points
4618 Posts
Re: Reading an XML file
Jan 23, 2013 03:34 PM|LINK
No problem! I'm glad you got everything to work out :)
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Reading an XML file
Jan 25, 2013 12:47 AM|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.