3

I'm having troubles reading values from an xml file. Here is the xml file:

<root>
    <defaultGroups name="Sikker">
        <group name="0ASK" />
        <group name="0ASKAPP" />
        <group name="0ASKFELLES" />
        <group name="0SYSAPP" />
        <group name="0SYSAPPoffice" />
        <group name="10WTS" />
    </defaultGroups>
    <defaultGroups name="Intern">
        <group name="11WTS" />
        <group name="1ASK" />
        <group name="1ASKAPP" />
        <group name="1ASKFELLES" />
        <group name="Domain Users" />
        <group name="Askvoll brukere" />
        <group name="1SYSAPP" />
        <group name="1SYSAPPAdobeReader" />
        <group name="1SYSAPPEXCEL" />
        <group name="1SYSAPPIEXPLORER" />
        <group name="1SYSAPPOUTLOOK" />
        <group name="1SYSAPPPOWERPOINT" />
        <group name="1SYSAPPWORD" />
    </defaultGroups>
</root>

With the function shown below, I'm supposed to only read the values from <defaultGroups name="Sikker">. I do get the first value: "0ASK", but not the rest. Can someone please help me with this? (I'm new to Linq)

This is the C# function I use:

public string GetSikkerSoneDefaultGroups(string companyName)
    {
        string sikkerSone = "";

        XDocument doc = XDocument.Load("xml\\defaults\\" + companyName + ".xml");
        var groups = from defaultGroups in doc.Descendants("defaultGroups")
                     where defaultGroups.Attribute("name").Value == "Sikker"
                     select new
                     {                             
                         g = defaultGroups.Element("group").Attribute("name").Value
                     };

        foreach (var group in groups)
        {
            sikkerSone += group.g + ";";
        }

        doc = null;

        return sikkerSone;

    }

2 Answers 2

2

You're using:

g = defaultGroups.Element("group").Attribute("name").Value

which only selects the first child element of defaultGroups. I think we can simplify this quite a bit, as the way you're approaching it you would need a subquery. Why not get the group elements directly?

var groups = from defaultGroup in doc.Descendants("group")
             where defaultGroup.Parent.Attribute("name").Value == "Sikker"
             select defaultGroup.Attribute("name").Value;


// Make it into a string
foreach (var group in groups)
{
     sikkerSone += group + ";";
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much womp! This worked exactly as i wanted :) Perfect!
0
string sikkerSone = string.Join(";",
   (from dg in doc.Descendants("defaultGroups")
    where dg.Attribute("name") == "Sikker"
    from g in dg.Elements("group")
    select (string)g.Attribute("name"))
   .ToArray());

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.