Irina_dusha
asked on
C# - How to find Elements from XML file
Hi , I am beginner wih C# and don't know exactly how to find elements from XML .
I have following XML file :
<?xml version="1.0" standalone="yes"?>
<ProjectDataSet xmlns="http://schemas.microsoft.com/office/project/server/webservices/ProjectDataSet/">
<Project>
</Project>
<Task>
<PROJ_UID>24582330-5950-43 d9-a3f2-a6 c59587edec </PROJ_UID >
<TASK_UID>315b072f-9f1b-4c 0b-a852-f8 d0b0bd7e59 </TASK_UID >
<TASK_PARENT_UID>215b072f- 9f1b-4c0b- a852-f8d0b 0bd7e59</T ASK_PARENT _UID>
<TASK_NAME>Task1</TASK_NAM E>
</Task>
<Task>
<PROJ_UID>24582330-5950-43 d9-a3f2-a6 c59587edec </PROJ_UID >
<TASK_UID>415b072f-9f1b-4c 0b-a852-f8 d0b0bd7e59 </TASK_UID >
<TASK_PARENT_UID>215b072f- 9f1b-4c0b- a852-f8d0b 0bd7e59</T ASK_PARENT _UID>
<TASK_NAME>Task2</TASK_NAM E>
</Task>
</ProjectDataSet>
I want to print all TASK_UID from all Tasks ,it's mean output should be :
315b072f-9f1b-4c0b-a852-f8 d0b0bd7e59
415b072f-9f1b-4c0b-a852-f8 d0b0bd7e59
Please help me write this in C#
I have following XML file :
<?xml version="1.0" standalone="yes"?>
<ProjectDataSet xmlns="http://schemas.microsoft.com/office/project/server/webservices/ProjectDataSet/">
<Project>
</Project>
<Task>
<PROJ_UID>24582330-5950-43
<TASK_UID>315b072f-9f1b-4c
<TASK_PARENT_UID>215b072f-
<TASK_NAME>Task1</TASK_NAM
</Task>
<Task>
<PROJ_UID>24582330-5950-43
<TASK_UID>415b072f-9f1b-4c
<TASK_PARENT_UID>215b072f-
<TASK_NAME>Task2</TASK_NAM
</Task>
</ProjectDataSet>
I want to print all TASK_UID from all Tasks ,it's mean output should be :
315b072f-9f1b-4c0b-a852-f8
415b072f-9f1b-4c0b-a852-f8
Please help me write this in C#
ASKER
It's not what I want ,
I need to receive TASK_UID ONLY from <TASK Element>
I need to receive TASK_UID ONLY from <TASK Element>
I've not tested the code so, sorry for errors.
PrintFunction must be the printing function that you want to use.
XML_Filename the name of the file that you want to open
you should write something like :
PrintFunction must be the printing function that you want to use.
XML_Filename the name of the file that you want to open
you should write something like :
DataTable dt = New DataTable();
dt.ReadXml(XML_filename);
for (i=0; i<dt.rows.count-1; i++){
printfunction(dt.rows(i)("TASK_UID");
}
Hi
smal example
in your case just change
oNodes = objNodes.SelectNodes("//Ta sk");
dtbl.Columns.Add(new DataColumn("UID", typeof(string)));
dr[0] = node.Attributes.GetNamedIt em("TASK_U ID").Value ;
vbturbo
smal example
in your case just change
oNodes = objNodes.SelectNodes("//Ta
dtbl.Columns.Add(new DataColumn("UID", typeof(string)));
dr[0] = node.Attributes.GetNamedIt
vbturbo
private void GetXmlData() {
XmlTextReader reader = new System.Xml.XmlTextReader("http://www.nationalbanken.dk/dndk/valuta.nsf/valuta.xml");
DataTable dtbl = new DataTable();
DataRow dr;
DataView dv = new DataView(dtbl);
System.Xml.XmlDocument objNodes = new System.Xml.XmlDocument();
objNodes.Load(reader);
System.Xml.XmlNodeList oNodes;
oNodes = objNodes.SelectNodes("//currency");
dtbl.Columns.Add(new DataColumn("Valuta", typeof(string)));
dtbl.Columns.Add(new DataColumn("Kurs", typeof(string)));
System.Xml.XmlNode node;
foreach (node in oNodes) {
dr = dtbl.NewRow();
dr[0] = node.Attributes.GetNamedItem("desc").Value;
dr[1] = node.Attributes.GetNamedItem("rate").Value;
dtbl.Rows.Add(dr);
}
dgvalutakurser.DataSource = dv;
// dgvalutakurser.DataBind()
}
i am submitting a sample code which will help you in reterieving what you want from the xml
i am assuming that you will change the PATH in the Load function of the XML Document class
then i am creating a node list for all the TASK_UID nodes and then looping through them
I hope this helps
i am assuming that you will change the PATH in the Load function of the XML Document class
then i am creating a node list for all the TASK_UID nodes and then looping through them
I hope this helps
XmlDocument xml = new XmlDocument();
xml.Load("XMLPATH");
XmlNodeList nodeList = xml.SelectNodes("//ProjectDataSet/Task/TASK_UID");
foreach (XmlNode node in nodeList)
{
System.Diagnostics.Debug.WriteLine(node.InnerText);
}
ASKER
vbturbo: ,ragi0017,
it's work only if my xml doesn't have xmlns element in the ProjectDataSet,but in my case
XmlNodeList nodeList = xml.SelectNodes("//Project DataSet/Ta sk/TASK_UI D"); //always return 0 nodes because of xmlns.If I delete it ,everything working.
How can I solve this ?
Thanks a lot for a help
it's work only if my xml doesn't have xmlns element in the ProjectDataSet,but in my case
XmlNodeList nodeList = xml.SelectNodes("//Project
How can I solve this ?
Thanks a lot for a help
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
i have tried changing the string to this as well although nodeList is still empty:
XmlNodeList nodeList = xml.SelectNodes("//y:feed/ y:entry/y: id", ns);
XmlNodeList nodeList = xml.SelectNodes("//y:feed/
http://www.c-sharpcorner.com/UploadFile/bulentozkir/ReadingXmlUsingXmlDocument11142005010141AM/ReadingXmlUsingXmlDocument.aspx
and
http://www.thescripts.com/forum/thread176368.html