0

I've been struggling to find out how to select multiple values from an XML file, compare them to a special value and then do something. So far I just managed to select a single value but I also need a different one in the same select, I hope you can assist me

XML Structure

<?xml version="1.0" encoding="utf-8"?>
<userConnectionSettings version="1" lastApplicationUrl="xxx" lastIdentity="yyy">
    <application url="xxx" lastFolderId="zzz">
        <user name="test" domain="domain.tld" lastFolderId="yyy" />
    </application>
</userConnectionSettings>

Now basically, what i want to do is read the lastApplicationURL and the domain value. I managed to do the lastApplicationURL but i can't seem to select the domain and i don't know how to get that value properly. Here's my code :

XDocument foDoc = XDocument.Load(FrontOfficePath);

foreach (var FOurl in foDoc.Descendants("userConnectionSettings"))
{
    string FOappURL = (string)FOurl.Attribute("lastApplicationUrl");

    if (FOappURL == "something")
    {
        TODO
    }
    else
    {
         TODO
    }
}
2
  • 1
    Have you tried FOurl.Element("application").Element("user").Attribute("domain")? Commented May 22, 2020 at 10:50
  • That did the trick, thank you! Commented May 22, 2020 at 12:13

2 Answers 2

1

You can select domain attribute, in two different ways :
1 - Like @Juharr comment :

foreach (var FOurl in foDoc.Descendants("userConnectionSettings"))
{
    string domain = FOurl
        .Element("application")
        .Element("user")
        .Attribute("domain")
        .Value;
....
}

Or, by getting descendant of application and select the first item, like :

foreach (var FOurl in foDoc.Descendants("userConnectionSettings"))
{
    string domain = FOurl.Descendants("application")
        .Select(x => x.Element("user").Attribute("domain").Value)
        .FirstOrDefault();
....
}

i hope you find this helpful.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhh i understand, perfect, thank you very much for the clarification. I wasn't aware you had to select it like this (via Attribute). Worked perfectly :)!
1

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            List<Application> applications = doc.Descendants("application").Select(x => new Application()
            {
                url = (string)x.Attribute("url"),
                id = (string)x.Attribute("lastFolderId"),
                name = (string)x.Element("user").Attribute("name"),
                domain = (string)x.Element("user").Attribute("domain"),
                folder = (string)x.Element("user").Attribute("lastFolderId")
            }).ToList();
        }
    }
    public class Application
    {
        public string url { get; set; }
        public string id { get; set; }
        public string name { get; set; }
        public string domain { get; set; }
        public string folder { get; set; }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.