Link to home
Start Free TrialLog in
Avatar of Irina_dusha
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-43d9-a3f2-a6c59587edec</PROJ_UID>
    <TASK_UID>315b072f-9f1b-4c0b-a852-f8d0b0bd7e59</TASK_UID>
    <TASK_PARENT_UID>215b072f-9f1b-4c0b-a852-f8d0b0bd7e59</TASK_PARENT_UID>
    <TASK_NAME>Task1</TASK_NAME>
</Task>
  <Task>
    <PROJ_UID>24582330-5950-43d9-a3f2-a6c59587edec</PROJ_UID>
    <TASK_UID>415b072f-9f1b-4c0b-a852-f8d0b0bd7e59</TASK_UID>
    <TASK_PARENT_UID>215b072f-9f1b-4c0b-a852-f8d0b0bd7e59</TASK_PARENT_UID>
    <TASK_NAME>Task2</TASK_NAME>
</Task>
</ProjectDataSet>

I want to print all TASK_UID from all Tasks ,it's mean output should be :
315b072f-9f1b-4c0b-a852-f8d0b0bd7e59
415b072f-9f1b-4c0b-a852-f8d0b0bd7e59

Please help me write this in C#
Avatar of Ashish Patel
Ashish Patel
Flag of India image

Avatar of Irina_dusha
Irina_dusha

ASKER

It's not what I want ,
I need to receive TASK_UID ONLY from  <TASK Element>



Avatar of sognoct
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 :


DataTable dt = New DataTable();
dt.ReadXml(XML_filename);
for (i=0; i<dt.rows.count-1; i++){
  printfunction(dt.rows(i)("TASK_UID");
}

Open in new window

Hi

smal example

in your case just change
oNodes = objNodes.SelectNodes("//Task");

dtbl.Columns.Add(new DataColumn("UID", typeof(string)));
dr[0] = node.Attributes.GetNamedItem("TASK_UID").Value;

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()
    }

Open in new window

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
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);
}

Open in new window

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("//ProjectDataSet/Task/TASK_UID"); //always return 0 nodes because of xmlns.If I delete it ,everything working.
How can I solve this ?

Thanks a lot for a help
ASKER CERTIFIED SOLUTION
Avatar of Anurag Thakur
Anurag Thakur
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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);