0

I'm trying to build a datastructure based on xml content.

The structure looks like that:

Dictionary<string, List<Dictionary<string, string>>>

The XML looks like that:

<Table name="testTable">
<Row>
  <Column name="test01" value="2029" />
  <Column name="test02" value="2029" />
</Row>
<Row>
  <Column name="test01" value="2029" />
  <Column name="test02" value="2029" />
</Row>
</Table>
<Table name="testTable01">
<Row>
  <Column name="test01" value="2029" />
  <Column name="test02" value="2029" />
</Row>
<Row>
  <Column name="test01" value="2029" />
  <Column name="test02" value="2029" />
</Row>
</Table>

It should result in something like that:

Dictionary<tableName, List<Dictionary<columnName, columnValue>>>

It's no problem to do that with some nested foreach-loops, but I'm searching for a way to do that "in one line" with the LINQ extension methods. How could I do that?

1
  • I only tried the nested loops and that worked. But now I'am searching for a "more elegant" solution. Commented Sep 13, 2012 at 19:40

2 Answers 2

4

Sounds like you want something like:

var tables = doc.Descendants("Table")
                .ToDictionary(t => (string) t.Attribute("name"),
                              t => ExtractRowsFromTable(t));

...

private static List<Dictionary<string, string>> ExtractRowsFromTable(XElement table)
{
    return table.Elements("Row")
                .Select(row => row.Elements("Column")
                                  .ToDictionary(c => (string) c.Attribute("name"),
                                                c => (string) c.Attribute("value"))
                .ToList();
}

You could do all of this in one line, basically inlining ExtractRowsFromTable - but I really wouldn't.

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

Comments

1
string xml = 
        @"<Root>
            <Table name=""testTable"">
                <Row>
                    <Column name=""test01"" value=""2029"" />
                    <Column name=""test02"" value=""2029"" />
                </Row>
                <Row>
                    <Column name=""test01"" value=""2029"" />
                    <Column name=""test02"" value=""2029"" />
                </Row>
            </Table>
            <Table name=""testTable2"">
                <Row>
                    <Column name=""test01"" value=""2029"" />
                    <Column name=""test02"" value=""2029"" />
                </Row>
                <Row>
                    <Column name=""test01"" value=""2029"" />
                    <Column name=""test02"" value=""2029"" />
                </Row>
            </Table>
        </Root>";
XDocument xDoc = XDocument.Parse(xml);
var table = xDoc
        .Descendants("Table")
        .Select(t => new
        {
            Name = t.Attribute("name").Value,
            Rows = t.Descendants("Row")
                    .Select(r=> r.Descendants("Column")
                                 .ToDictionary(c=>c.Attribute("name").Value,
                                               c=>c.Attribute("value").Value))
                    .ToList()
        })
        .ToList();

2 Comments

Thank you, I will take a closer look at your solution.
Currently you're building a list as the "outer" part, when the OP requested a dictionary. Easy enough to fix, of course :)

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.