locked
Best way loop XML RRS feed

  • Question

  • User-1830624064 posted

    XML is new for me and after a day reading the Internet and playing with samples from the internet I’m even more confused.

    My PC application gets XML data from a webservice like:

    <projects>
      <project name="Project 1" ID="PRO0001">
        <resass_Synchronisation>1</resass_Synchronisation>
        <rel_DisplayName>Arno</rel_DisplayName>
        <rel_EMail>arno@project.nl</rel_EMail>
        <rel_Username>Arno</rel_Username>
        <rel_Password>11223344</rel_Password>
        <rel_status>1</rel_status>
      </project>
      <project name="Project 2" ID="PRO0002">
        <resass_Synchronisation>1</resass_Synchronisation>
        <rel_DisplayName>Harry</rel_DisplayName>
        <rel_EMail>harry@123.com</rel_EMail>
        <rel_Username>Harry</rel_Username>
        <rel_Password>wweerrtt</rel_Password>
        <rel_status>1</rel_status>
      </project>
      <project name="Project 3" ID="PRO0003">
        <resass_Synchronisation>1</resass_Synchronisation>
        <rel_DisplayName>Ben</rel_DisplayName>
        <rel_Username>Ben@bankie.net</rel_Username>
        <rel_status>0</rel_status>
      </project>
    </projects>
    

    Now I need to loop all <projects>, extract the name- and ID-attribute and get all the sub-nodes. What is the best way of doing this?

    Thanks for your help.

     

    Arno 

     

    Tuesday, May 22, 2012 6:58 PM

Answers

  • User-1965752605 posted
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml.Linq;
    
    public partial class Default1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var doc = XDocument.Load(Server.MapPath("~/") + "xmlfile.xml");
            foreach (XElement xe in doc.Descendants("project"))
            {
                string name = xe.Attribute("name").Value.ToString();
                string ID = xe.Attribute("ID").Value.ToString();
                foreach (XNode xn in xe.Nodes())
                {
                    string childnodename = xn.ToString();
                }
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 23, 2012 12:18 AM

All replies

  • User-1965752605 posted
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Xml.Linq;
    
    public partial class Default1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var doc = XDocument.Load(Server.MapPath("~/") + "xmlfile.xml");
            foreach (XElement xe in doc.Descendants("project"))
            {
                string name = xe.Attribute("name").Value.ToString();
                string ID = xe.Attribute("ID").Value.ToString();
                foreach (XNode xn in xe.Nodes())
                {
                    string childnodename = xn.ToString();
                }
            }
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, May 23, 2012 12:18 AM
  • User-1965857832 posted
    XDocument xDocument = XDocument.Load(@"ur xml path");
    
    var projects = from project in xDocument.Descendants("project")
                    select new
                    {
                        Name = project.Attribute("name").Value,
                        ID = project.Attribute("ID").Value,
                        Resass_Synchronisation = project.Element("resass_Synchronisation") == null ? string.Empty : project.Element("resass_Synchronisation").Value,
                        Rel_DisplayName = project.Element("rel_DisplayName") == null ? string.Empty : project.Element("rel_DisplayName").Value,
                        Rel_EMail = project.Element("rel_EMail") == null ? string.Empty : project.Element("rel_EMail").Value,
                        Rel_Username = project.Element("rel_Username") == null ? string.Empty : project.Element("rel_Username").Value,
                        Rel_Password = project.Element("rel_Password") == null ? string.Empty : project.Element("rel_Password").Value,
                        Rel_status = project.Element("rel_status") == null ? string.Empty : project.Element("rel_status").Value
                    };
    
    
    foreach(var project in projects)
    {
        var name = project.Name;
        var id = project.ID;
        var resass_Synchronisation = project.Resass_Synchronisation;
        var rel_DisplayName = project.Rel_DisplayName;
        var rel_EMail = project.Rel_EMail;
        var rel_Username = project.Rel_Username;
        var rel_Password = project.Rel_Password;
        var rel_status = project.Rel_status;
    }

    Wednesday, May 23, 2012 5:05 AM
  • User-1830624064 posted

    Thanks to both of you. It works and now I also understand what I did wrong.

    Both solutions mix XDocument and XElement. I thought it were different techniques.

     

    Thanks again.

     

    Arno

     

    Wednesday, May 23, 2012 5:53 AM